Skip to content

Commit

Permalink
feat(adapters): Extract common functionalites for reconcilation of na…
Browse files Browse the repository at this point in the history
…tive policies

Signed-off-by: Anurag Rajawat <[email protected]>
  • Loading branch information
anurag-rajawat committed Feb 7, 2024
1 parent c460dc3 commit 98355cc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/adapter/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 Authors of Nimbus

package common

type Request struct {
Name string
Namespace string
}
38 changes: 38 additions & 0 deletions pkg/adapter/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions pkg/adapter/util/nimbuspolicy_util.go
Original file line number Diff line number Diff line change
@@ -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], "-")
}
12 changes: 12 additions & 0 deletions pkg/adapter/util/watcher_util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 98355cc

Please sign in to comment.