Skip to content

Commit

Permalink
Merge pull request #114 from MochaCaffe/master
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislusf authored Oct 9, 2024
2 parents 6fa4c24 + ce70167 commit 049dc0b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"crypto/tls"
"flag"
monitorv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand Down Expand Up @@ -48,6 +49,7 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(seaweedv1.AddToScheme(scheme))
utilruntime.Must(monitorv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

Expand Down
15 changes: 15 additions & 0 deletions deploy/helm/templates/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rules:
- pods
verbs:
- get
- watch
- list
- apiGroups:
- ""
Expand Down Expand Up @@ -93,3 +94,17 @@ rules:
- get
- patch
- update
{{- if .Values.serviceMonitor.enabled }}
- apiGroups:
- monitoring.coreos.com
resources:
- servicemonitors
verbs:
- get
- list
- watch
- patch
- update
- create
- delete
{{- end }}
20 changes: 11 additions & 9 deletions internal/controller/controller_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"

monitorv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -14,7 +13,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -45,9 +43,9 @@ func (r *SeaweedReconciler) CreateOrUpdate(obj runtime.Object, mergeFn MergeFn)
// 1. try to create and see if there is any conflicts
err := r.Create(context.TODO(), desired)
if errors.IsAlreadyExists(err) {

// 2. object has already existed, merge our desired changes to it
existing, err := EmptyClone(obj)
existing, err := r.EmptyClone(obj)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -239,6 +237,7 @@ func (r *SeaweedReconciler) CreateOrUpdateConfigMap(configMap *corev1.ConfigMap)
}

func (r *SeaweedReconciler) CreateOrUpdateServiceMonitor(serviceMonitor *monitorv1.ServiceMonitor) (*monitorv1.ServiceMonitor, error) {

result, err := r.CreateOrUpdate(serviceMonitor, func(existing, desired runtime.Object) error {
existingServiceMonitor := existing.(*monitorv1.ServiceMonitor)
desiredServiceMonitor := desired.(*monitorv1.ServiceMonitor)
Expand All @@ -260,31 +259,34 @@ func (r *SeaweedReconciler) CreateOrUpdateServiceMonitor(serviceMonitor *monitor
}

// EmptyClone create an clone of the resource with the same name and namespace (if namespace-scoped), with other fields unset
func EmptyClone(obj runtime.Object) (runtime.Object, error) {
func (r *SeaweedReconciler) EmptyClone(obj runtime.Object) (runtime.Object, error) {

meta, ok := obj.(metav1.Object)
if !ok {
return nil, fmt.Errorf("Obj %v is not a metav1.Object, cannot call EmptyClone", obj)
}
gvk, err := InferObjectKind(obj)

gvk, err := r.InferObjectKind(obj)
if err != nil {
return nil, err
}
inst, err := scheme.Scheme.New(gvk)
inst, err := r.Scheme.New(gvk)
if err != nil {
return nil, err
}
instMeta, ok := inst.(metav1.Object)
if !ok {
return nil, fmt.Errorf("New instatnce %v created from scheme is not a metav1.Object, EmptyClone failed", inst)
}

instMeta.SetName(meta.GetName())
instMeta.SetNamespace(meta.GetNamespace())
return inst, nil
}

// InferObjectKind infers the object kind
func InferObjectKind(obj runtime.Object) (schema.GroupVersionKind, error) {
gvks, _, err := scheme.Scheme.ObjectKinds(obj)
func (r *SeaweedReconciler) InferObjectKind(obj runtime.Object) (schema.GroupVersionKind, error) {
gvks, _, err := r.Scheme.ObjectKinds(obj)
if err != nil {
return schema.GroupVersionKind{}, err
}
Expand Down

0 comments on commit 049dc0b

Please sign in to comment.