Skip to content

Commit

Permalink
Remove old component interface (#1433)
Browse files Browse the repository at this point in the history
- old and unused Component interface removed
- UpdatePrometheusConfig function moved to pkg/services/monitoring
  • Loading branch information
lburgazzoli authored Dec 9, 2024
1 parent 1adbbfd commit f9a2b9b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 198 deletions.
1 change: 0 additions & 1 deletion Dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
25 changes: 0 additions & 25 deletions apis/datasciencecluster/v1/datasciencecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
65 changes: 0 additions & 65 deletions components/zz_generated.deepcopy.go

This file was deleted.

21 changes: 0 additions & 21 deletions docs/api-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
98 changes: 12 additions & 86 deletions components/component.go → pkg/services/monitoring/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,101 +1,26 @@
// +groupName=datasciencecluster.opendatahub.io
package components
package monitoring

import (
"context"
"os"
"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/<tag/branch>
// +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 <component>.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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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" {
Expand All @@ -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
}

0 comments on commit f9a2b9b

Please sign in to comment.