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 +}