From f9a2b9bab6c68c0e495da4d84f804626ac3caa43 Mon Sep 17 00:00:00 2001 From: Luca Burgazzoli Date: Mon, 9 Dec 2024 19:30:58 +0100 Subject: [PATCH] Remove old component interface (#1433) - old and unused Component interface removed - UpdatePrometheusConfig function moved to pkg/services/monitoring --- Dockerfiles/Dockerfile | 1 - .../v1/datasciencecluster_types.go | 25 ----- components/zz_generated.deepcopy.go | 65 ------------ docs/api-overview.md | 21 ---- .../services/monitoring/prometheus.go | 98 +++---------------- 5 files changed, 12 insertions(+), 198 deletions(-) delete mode 100644 components/zz_generated.deepcopy.go rename components/component.go => pkg/services/monitoring/prometheus.go (51%) diff --git a/Dockerfiles/Dockerfile b/Dockerfiles/Dockerfile index a74e73a1135..cd48396c06c 100644 --- a/Dockerfiles/Dockerfile +++ b/Dockerfiles/Dockerfile @@ -28,7 +28,6 @@ RUN go mod download # Copy the go source COPY apis/ apis/ -COPY components/ components/ COPY controllers/ controllers/ COPY main.go main.go COPY pkg/ pkg/ diff --git a/apis/datasciencecluster/v1/datasciencecluster_types.go b/apis/datasciencecluster/v1/datasciencecluster_types.go index fa4629ada72..78ece78c8c6 100644 --- a/apis/datasciencecluster/v1/datasciencecluster_types.go +++ b/apis/datasciencecluster/v1/datasciencecluster_types.go @@ -17,15 +17,11 @@ limitations under the License. package v1 import ( - "errors" - "reflect" - conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" componentApi "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1alpha1" - "github.com/opendatahub-io/opendatahub-operator/v2/components" "github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster" ) @@ -134,24 +130,3 @@ type DataScienceClusterList struct { func init() { SchemeBuilder.Register(&DataScienceCluster{}, &DataScienceClusterList{}) } - -func (d *DataScienceCluster) GetComponents() ([]components.ComponentInterface, error) { - var allComponents []components.ComponentInterface - - c := &d.Spec.Components - - definedComponents := reflect.ValueOf(c).Elem() - for i := 0; i < definedComponents.NumField(); i++ { - c := definedComponents.Field(i) - if c.CanAddr() { - component, ok := c.Addr().Interface().(components.ComponentInterface) - if !ok { - return allComponents, errors.New("this is not a pointer to ComponentInterface") - } - - allComponents = append(allComponents, component) - } - } - - return allComponents, nil -} diff --git a/components/zz_generated.deepcopy.go b/components/zz_generated.deepcopy.go deleted file mode 100644 index fdcf0aa8cf7..00000000000 --- a/components/zz_generated.deepcopy.go +++ /dev/null @@ -1,65 +0,0 @@ -//go:build !ignore_autogenerated - -/* -Copyright 2023. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Code generated by controller-gen. DO NOT EDIT. - -package components - -import ( - "github.com/opendatahub-io/opendatahub-operator/v2/apis/common" -) - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Component) DeepCopyInto(out *Component) { - *out = *in - if in.DevFlags != nil { - in, out := &in.DevFlags, &out.DevFlags - *out = new(DevFlags) - (*in).DeepCopyInto(*out) - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Component. -func (in *Component) DeepCopy() *Component { - if in == nil { - return nil - } - out := new(Component) - in.DeepCopyInto(out) - return out -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *DevFlags) DeepCopyInto(out *DevFlags) { - *out = *in - if in.Manifests != nil { - in, out := &in.Manifests, &out.Manifests - *out = make([]common.ManifestsConfig, len(*in)) - copy(*out, *in) - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DevFlags. -func (in *DevFlags) DeepCopy() *DevFlags { - if in == nil { - return nil - } - out := new(DevFlags) - in.DeepCopyInto(out) - return out -} diff --git a/docs/api-overview.md b/docs/api-overview.md index a2c50167c49..80f13a1f655 100644 --- a/docs/api-overview.md +++ b/docs/api-overview.md @@ -1440,27 +1440,6 @@ _Appears in:_ - -#### DevFlags - - - -DevFlags defines list of fields that can be used by developers to test customizations. This is not recommended -to be used in production environment. - - - -_Appears in:_ -- [Component](#component) - -| Field | Description | Default | Validation | -| --- | --- | --- | --- | -| `manifests` _[ManifestsConfig](#manifestsconfig) array_ | List of custom manifests for the given component | | | - - - - - ## datasciencecluster.opendatahub.io/v1 diff --git a/components/component.go b/pkg/services/monitoring/prometheus.go similarity index 51% rename from components/component.go rename to pkg/services/monitoring/prometheus.go index 5de1072e906..d09d093b675 100644 --- a/components/component.go +++ b/pkg/services/monitoring/prometheus.go @@ -1,5 +1,4 @@ -// +groupName=datasciencecluster.opendatahub.io -package components +package monitoring import ( "context" @@ -7,95 +6,21 @@ import ( "path/filepath" "strings" - "github.com/go-logr/logr" - operatorv1 "github.com/openshift/api/operator/v1" "gopkg.in/yaml.v2" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" + logf "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/opendatahub-io/opendatahub-operator/v2/apis/common" - dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1" - "github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster" + "github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy" ) -// Component struct defines the basis for each OpenDataHub component configuration. -// +kubebuilder:object:generate=true -type Component struct { - // Set to one of the following values: - // - // - "Managed" : the operator is actively managing the component and trying to keep it active. - // It will only upgrade the component if it is safe to do so - // - // - "Removed" : the operator is actively managing the component and will not install it, - // or if it is installed, the operator will try to remove it - // - // +kubebuilder:validation:Enum=Managed;Removed - ManagementState operatorv1.ManagementState `json:"managementState,omitempty"` - // Add any other common fields across components below - - // Add developer fields - // +optional - // +operator-sdk:csv:customresourcedefinitions:type=spec,order=2 - DevFlags *DevFlags `json:"devFlags,omitempty"` -} - -func (c *Component) Init(_ context.Context, _ cluster.Platform) error { - return nil -} - -func (c *Component) GetManagementState() operatorv1.ManagementState { - return c.ManagementState -} - -func (c *Component) Cleanup(_ context.Context, _ client.Client, _ metav1.Object, _ *dsciv1.DSCInitializationSpec) error { - // noop - return nil -} - -// DevFlags defines list of fields that can be used by developers to test customizations. This is not recommended -// to be used in production environment. -// +kubebuilder:object:generate=true -type DevFlags struct { - // List of custom manifests for the given component - // +optional - Manifests []common.ManifestsConfig `json:"manifests,omitempty"` -} - -type ManifestsConfig struct { - // uri is the URI point to a git repo with tag/branch. e.g. https://github.com/org/repo/tarball/ - // +optional - // +kubebuilder:default:="" - // +operator-sdk:csv:customresourcedefinitions:type=spec,order=1 - URI string `json:"uri,omitempty"` - - // contextDir is the relative path to the folder containing manifests in a repository, default value "manifests" - // +optional - // +kubebuilder:default:="manifests" - // +operator-sdk:csv:customresourcedefinitions:type=spec,order=2 - ContextDir string `json:"contextDir,omitempty"` - - // sourcePath is the subpath within contextDir where kustomize builds start. Examples include any sub-folder or path: `base`, `overlays/dev`, `default`, `odh` etc. - // +optional - // +kubebuilder:default:="" - // +operator-sdk:csv:customresourcedefinitions:type=spec,order=3 - SourcePath string `json:"sourcePath,omitempty"` -} - -type ComponentInterface interface { - Init(ctx context.Context, platform cluster.Platform) error - ReconcileComponent(ctx context.Context, cli client.Client, logger logr.Logger, - owner metav1.Object, DSCISpec *dsciv1.DSCInitializationSpec, platform cluster.Platform, currentComponentStatus bool) error - Cleanup(ctx context.Context, cli client.Client, owner metav1.Object, DSCISpec *dsciv1.DSCInitializationSpec) error - GetComponentName() string - GetManagementState() operatorv1.ManagementState - OverrideManifests(ctx context.Context, platform cluster.Platform) error - UpdatePrometheusConfig(cli client.Client, logger logr.Logger, enable bool, component string) error -} +var ( + prometheusConfigPath = filepath.Join(deploy.DefaultManifestPath, "monitoring", "prometheus", "apps", "prometheus-configs.yaml") +) // UpdatePrometheusConfig update prometheus-configs.yaml to include/exclude .rules // parameter enable when set to true to add new rules, when set to false to remove existing rules. -func (c *Component) UpdatePrometheusConfig(_ client.Client, logger logr.Logger, enable bool, component string) error { - prometheusconfigPath := filepath.Join("/opt/manifests", "monitoring", "prometheus", "apps", "prometheus-configs.yaml") +func UpdatePrometheusConfig(ctx context.Context, _ client.Client, enable bool, component string) error { + l := logf.FromContext(ctx) // create a struct to mock poremtheus.yml type ConfigMap struct { @@ -134,12 +59,13 @@ func (c *Component) UpdatePrometheusConfig(_ client.Client, logger logr.Logger, ModelRegistryARules string `yaml:"model-registry-operator-alerting.rules"` } `yaml:"data"` } + var configMap ConfigMap // prometheusContent will represent content of prometheus.yml due to its dynamic struct var prometheusContent map[interface{}]interface{} // read prometheus.yml from local disk /opt/mainfests/monitoring/prometheus/apps/ - yamlData, err := os.ReadFile(prometheusconfigPath) + yamlData, err := os.ReadFile(prometheusConfigPath) if err != nil { return err } @@ -166,7 +92,7 @@ func (c *Component) UpdatePrometheusConfig(_ client.Client, logger logr.Logger, } } } else { // to remove component rules if it is there - logger.Info("Removing prometheus rule: " + component + "*.rules") + l.Info("Removing prometheus rule: " + component + "*.rules") if ruleList, ok := prometheusContent["rule_files"].([]interface{}); ok { for i, item := range ruleList { if rule, isStr := item.(string); isStr && rule == component+"*.rules" { @@ -192,7 +118,7 @@ func (c *Component) UpdatePrometheusConfig(_ client.Client, logger logr.Logger, } // Write the modified content back to the file - err = os.WriteFile(prometheusconfigPath, newyamlData, 0) + err = os.WriteFile(prometheusConfigPath, newyamlData, 0) return err }