From 98355cc437ea0b6605d0420c7e5c84a1818e2dab Mon Sep 17 00:00:00 2001 From: Anurag Rajawat Date: Wed, 7 Feb 2024 20:13:26 +0530 Subject: [PATCH] feat(adapters): Extract common functionalites for reconcilation of native policies Signed-off-by: Anurag Rajawat --- pkg/adapter/common/common.go | 9 +++++++ pkg/adapter/k8s/client.go | 38 +++++++++++++++++++++++++++ pkg/adapter/util/nimbuspolicy_util.go | 13 +++++++++ pkg/adapter/util/watcher_util.go | 12 +++++++++ 4 files changed, 72 insertions(+) create mode 100644 pkg/adapter/common/common.go create mode 100644 pkg/adapter/util/nimbuspolicy_util.go create mode 100644 pkg/adapter/util/watcher_util.go diff --git a/pkg/adapter/common/common.go b/pkg/adapter/common/common.go new file mode 100644 index 00000000..6b5de7b5 --- /dev/null +++ b/pkg/adapter/common/common.go @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2023 Authors of Nimbus + +package common + +type Request struct { + Name string + Namespace string +} diff --git a/pkg/adapter/k8s/client.go b/pkg/adapter/k8s/client.go index 3d4cc402..1423b227 100644 --- a/pkg/adapter/k8s/client.go +++ b/pkg/adapter/k8s/client.go @@ -51,3 +51,41 @@ func NewDynamicClient() dynamic.Interface { } return clientSet } + +// NewOrDie returns a new Kubernetes client and panics if there is an error in +// the config. +func NewOrDie(scheme *runtime.Scheme) client.Client { + config, err := rest.InClusterConfig() + if err != nil && errors.Is(err, rest.ErrNotInCluster) { + kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config") + config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) + if err != nil { + panic(fmt.Sprintf("failed to load kubeconfig '%v', error: %v\n", kubeconfig, err)) + } + } + k8sClient, err := client.New(config, client.Options{ + Scheme: scheme, + }) + if err != nil { + panic(fmt.Sprintf("failed to create client, error: %v", err)) + } + return k8sClient +} + +// NewDynamicClientOrDie returns a Dynamic Kubernetes client and panics if there +// is an error in the config. +func NewDynamicClientOrDie() dynamic.Interface { + config, err := rest.InClusterConfig() + if err != nil && errors.Is(err, rest.ErrNotInCluster) { + kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config") + config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) + if err != nil { + panic(err) + } + } + clientSet, err := dynamic.NewForConfig(config) + if err != nil { + panic(err) + } + return clientSet +} diff --git a/pkg/adapter/util/nimbuspolicy_util.go b/pkg/adapter/util/nimbuspolicy_util.go new file mode 100644 index 00000000..3889c0b1 --- /dev/null +++ b/pkg/adapter/util/nimbuspolicy_util.go @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2023 Authors of Nimbus + +package util + +import ( + "strings" +) + +func ExtractNpName(kspName string) string { + words := strings.Split(kspName, "-") + return strings.Join(words[:len(words)-1], "-") +} diff --git a/pkg/adapter/util/watcher_util.go b/pkg/adapter/util/watcher_util.go new file mode 100644 index 00000000..936fba90 --- /dev/null +++ b/pkg/adapter/util/watcher_util.go @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2023 Authors of Nimbus + +package util + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func IsOrphan(ownerRefs []metav1.OwnerReference, ownerKind string) bool { + return len(ownerRefs) == 0 || ownerRefs[0].Kind != ownerKind +}