Skip to content

Commit

Permalink
chore: make object counter conditional on CRD existence
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Oct 8, 2024
1 parent b42d932 commit 5c85628
Showing 1 changed file with 66 additions and 41 deletions.
107 changes: 66 additions & 41 deletions internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

operatorv1alpha1 "github.com/kong/gateway-operator/api/v1alpha1"
operatorv1beta1 "github.com/kong/gateway-operator/api/v1beta1"
"github.com/kong/gateway-operator/modules/manager/scheme"
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"
)
Expand Down Expand Up @@ -105,71 +106,95 @@ func createManager(
}
// Add cluster state workflow
{
checker := k8sutils.CRDChecker{Client: cl}

cpExists, err := checker.CRDExists(operatorv1beta1.ControlPlaneGVR())
if err != nil {
log.Info("failed to check if controlplane CRD exists", "error", err)
}
aiGatewayExists, err := checker.CRDExists(operatorv1alpha1.AIGatewayGVR())
if err != nil {
log.Info("failed to check if aigateway CRD exists", "error", err)
}
dpExists, err := checker.CRDExists(operatorv1beta1.DataPlaneGVR())
if err != nil {
log.Info("failed to check if dataplane CRD exists", "error", err)
}

w, err := telemetry.NewClusterStateWorkflow(dyn, cl.RESTMapper())
if err != nil {
return nil, fmt.Errorf("failed to create cluster state workflow: %w", err)
}

// Add dataplane count provider to monitor number of dataplanes in the cluster.
p, err := NewDataPlaneCountProvider(dyn, cl.RESTMapper())
if err != nil {
log.Info("failed to create dataplane count provider", "error", err)
} else {
w.AddProvider(p)
if dpExists {
// Add dataplane count provider to monitor number of dataplanes in the cluster.
p, err := NewDataPlaneCountProvider(dyn, cl.RESTMapper())
if err != nil {
log.Info("failed to create dataplane count provider", "error", err)
} else {
w.AddProvider(p)
}
}

// Add controlplane count provider to monitor number of controlplanes in the cluster.
p, err = NewControlPlaneCountProvider(dyn, cl.RESTMapper())
if err != nil {
log.Info("failed to create controlplane count provider", "error", err)
} else {
w.AddProvider(p)
if cpExists {
// Add controlplane count provider to monitor number of controlplanes in the cluster.
p, err := NewControlPlaneCountProvider(dyn, cl.RESTMapper())
if err != nil {
log.Info("failed to create controlplane count provider", "error", err)
} else {
w.AddProvider(p)
}
}

checker := k8sutils.CRDChecker{Client: cl}
// AIGateway is optional so check if it exists before enabling the count provider.
if exists, err := checker.CRDExists(operatorv1alpha1.AIGatewayGVR()); err != nil {
log.Info("failed to check if aigateway CRD exists ", "error", err)
} else if exists {
if aiGatewayExists {
// Add aigateway count provider to monitor number of aigateways in the cluster.
p, err = NewAIgatewayCountProvider(dyn, cl.RESTMapper())
p, err := NewAIgatewayCountProvider(dyn, cl.RESTMapper())
if err != nil {
log.Info("failed to create aigateway count provider", "error", err)
} else {
w.AddProvider(p)
}
}

// Add dataplane count not from gateway.
p, err = NewStandaloneDataPlaneCountProvider(cl)
if err != nil {
log.Info("failed to create standalone dataplane count provider", "error", err)
} else {
w.AddProvider(p)
if dpExists {
// Add dataplane count not from gateway.
p, err := NewStandaloneDataPlaneCountProvider(cl)
if err != nil {
log.Info("failed to create standalone dataplane count provider", "error", err)
} else {
w.AddProvider(p)
}
}

// Add controlplane count not from gateway.
p, err = NewStandaloneControlPlaneCountProvider(cl)
if err != nil {
log.Info("failed to create standalone controlplane count provider", "error", err)
} else {
w.AddProvider(p)
if cpExists {
// Add controlplane count not from gateway.
p, err := NewStandaloneControlPlaneCountProvider(cl)
if err != nil {
log.Info("failed to create standalone controlplane count provider", "error", err)
} else {
w.AddProvider(p)
}
}

// Add dataplane requested replicas count provider to monitor number of requested replicas for dataplanes.
p, err = NewDataPlaneRequestedReplicasCountProvider(cl)
if err != nil {
log.Info("failed to create dataplane requested replicas count provider", "error", err)
} else {
w.AddProvider(p)
if dpExists {
// Add dataplane requested replicas count provider to monitor number of requested replicas for dataplanes.
p, err := NewDataPlaneRequestedReplicasCountProvider(cl)
if err != nil {
log.Info("failed to create dataplane requested replicas count provider", "error", err)
} else {
w.AddProvider(p)
}
}

// Add controlplane requested replicas count provider to monitor number of requested replicas for controlplanes.
p, err = NewControlPlaneRequestedReplicasCountProvider(cl)
if err != nil {
log.Info("failed to create controlplane requested replicas count provider", "error", err)
} else {
w.AddProvider(p)
if cpExists {
// Add controlplane requested replicas count provider to monitor number of requested replicas for controlplanes.
p, err := NewControlPlaneRequestedReplicasCountProvider(cl)
if err != nil {
log.Info("failed to create controlplane requested replicas count provider", "error", err)
} else {
w.AddProvider(p)
}
}

m.AddWorkflow(w)
Expand Down

0 comments on commit 5c85628

Please sign in to comment.