Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default flag for dataviews #58

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Configuration options are documented in [chart README file](charts/eck-custom-re

## Upgrade guide

### From 0.7.0 to 0.8.0
The CRD for Kibana DataView was changed - the `defaultView` field was added, to flag if the Data View should be configured as default in target instance Kibana. To update the CRD manually, run:
```
kubectl apply --server-side -f https://raw.githubusercontent.com/xco-sk/eck-custom-resources/v0.8.0/config/crd/bases/kibana.eck.github.com_dataviews.yaml
```

### From 0.6.0 to 0.7.0
There is a new `ComponentTemplate` CRD present. To apply the CRD, run:
```
Expand Down
3 changes: 2 additions & 1 deletion apis/kibana.eck/v1alpha1/dataview_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
type DataViewSpec struct {
// +optional
TargetConfig CommonKibanaConfig `json:"targetInstance,omitempty"`

// DefaultView defines if this DataView is configured as default in Kibana
DefaultView *bool `json:"defaultView,omitempty"`
SavedObject `json:",inline"`
}

Expand Down
5 changes: 5 additions & 0 deletions apis/kibana.eck/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions charts/eck-custom-resources-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ maintainers:
email: [email protected]
url: https://github.com/xco-sk
type: application
version: 0.7.0
appVersion: 0.7.0
version: 0.8.0
appVersion: 0.8.0
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ spec:
properties:
body:
type: string
defaultView:
description: DefaultView defines if this DataView is configured as
default in Kibana
type: boolean
dependencies:
items:
properties:
Expand Down
1 change: 1 addition & 0 deletions config/samples/kibana.eck_v1alpha1_dataview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ spec:
dependencies:
- type: lens
name: lens-sample
defaultView: false
body: |
{
"title": "sample-index-*",
Expand Down
14 changes: 8 additions & 6 deletions docs/cr_data_view.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ See [Data Views APIs](https://www.elastic.co/guide/en/kibana/current/data-views-
## Fields

| Key | Type | Description | Default |
|-----------------------------|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------|
| `metadata.name` | string | Name of the Data View visualization, used also as its ID in Kibana | No default |
| `spec.space` | string | Name of the Kibana namespace to which the Data View is deployed to | No default (will be deployed to "default" namespace) |
| `spec.targetInstance.name` | string | Name of the [Kibana Instance](cr_kibana_instance.md) to which this DataView will be deployed to | The operator configuration |
| `spec.body` | string | Data View definition (the inner part of the requests) json | No default |
| `spec.dependencies` | List of objects | List of dependencies - the reconciler will wait for all resources from the list to be present in Kibana before deploying/updating this resource | - | |
| --------------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| `metadata.name` | string | Name of the Data View visualization, used also as its ID in Kibana | No default |
| `spec.space` | string | Name of the Kibana namespace to which the Data View is deployed to | No default (will be deployed to "default" namespace) |
| `spec.targetInstance.name` | string | Name of the [Kibana Instance](cr_kibana_instance.md) to which this DataView will be deployed to | The operator configuration |
| `spec.body` | string | Data View definition (the inner part of the requests) json | No default |
| `spec.defaultView` | boolean | Flag if the Data View should be configured as default in target instance Kibana | false |
| `spec.dependencies` | List of objects | List of dependencies - the reconciler will wait for all resources from the list to be present in Kibana before deploying/updating this resource | - | |
| `spec.dependencies[].space` | string | Kibana Space where to look for given resource | - |
| `spec.dependencies[].type` | string | Type of resource - one of `visualization, dashboard, search, index-pattern, lens` | - |
| `spec.dependencies[].name` | string | Name of resource | - |
Expand All @@ -39,6 +40,7 @@ spec:
dependencies:
- type: lens
name: lens-sample
defaultView: true
body: |
{
"title": "sample-index-*",
Expand Down
38 changes: 37 additions & 1 deletion utils/kibana/dataview_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func UpsertDataView(kClient Client, dataView kibanaeckv1alpha1.DataView) (ctrl.R
} else {
res, err = kClient.DoPost(formatDataViewUrl(dataView.Spec.Space), *modifiedBody)
}

if err != nil {
return utils.GetRequeueResult(), err
}
Expand All @@ -51,6 +50,11 @@ func UpsertDataView(kClient Client, dataView kibanaeckv1alpha1.DataView) (ctrl.R
return utils.GetRequeueResult(), fmt.Errorf("Non-success (%d) response: %s, ", res.StatusCode, string(resBody))
}

err = handleDefaultFlagOnUpsert(dataView, kClient)
if err != nil {
return utils.GetRequeueResult(), err
}

return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -130,3 +134,35 @@ func removeName(objectJson string, id string) (*string, error) {
sBody := string(marshalledBody)
return &sBody, nil
}

func handleDefaultFlagOnUpsert(dataview kibanaeckv1alpha1.DataView, kClient Client) error {
if dataview.Spec.DefaultView != nil && *dataview.Spec.DefaultView {
saveDefaultView(&dataview.Name, dataview.Spec.Space, kClient)
}
return nil
}

func saveDefaultView(dataViewName *string, space *string, kClient Client) error {
body := make(map[string]interface{})
body["data_view_id"] = dataViewName
body["force"] = true

marshalledBody, err := json.Marshal(body)
if err != nil {
return err
}
_, err = kClient.DoPost(formatDefaultDataViewUrl(space), string(marshalledBody))
return err
}

func getDefaultDataView(space *string, kClient Client) (*string, error) {
_, err = kClient.DoGet(formatDefaultDataViewUrl(space), string(marshalledBody))

}

func formatDefaultDataViewUrl(space *string) string {
if space == nil {
return "/api/data_views/default"
}
return fmt.Sprintf("/s/%s/api/data_views/default", *space)
}
Loading