Skip to content

Commit

Permalink
limit target namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-cvit committed Nov 4, 2024
1 parent b078490 commit 948e6de
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions cyclops-ctrl/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ PORT=8888
WATCH_NAMESPACE=cyclops
WATCH_NAMESPACE_HELM=
CYCLOPS_VERSION=v0.0.0
MODULE_TARGET_NAMESPACES=
18 changes: 17 additions & 1 deletion cyclops-ctrl/cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
"time"

_ "github.com/joho/godotenv/autoload"
Expand Down Expand Up @@ -74,7 +75,12 @@ func main() {
watchNamespace := getWatchNamespace()
helmWatchNamespace := getHelmWatchNamespace()

k8sClient, err := k8sclient.New(watchNamespace, helmWatchNamespace, zap.New(zap.UseFlagOptions(&opts)))
k8sClient, err := k8sclient.New(
watchNamespace,
helmWatchNamespace,
getModuleTargetNamespaces(),
zap.New(zap.UseFlagOptions(&opts)),
)
if err != nil {
fmt.Println("error bootstrapping Kubernetes client", err)
panic(err)
Expand Down Expand Up @@ -182,3 +188,13 @@ func getHelmWatchNamespace() string {
}
return value
}

func getModuleTargetNamespaces() []string {
envVar := os.Getenv("MODULE_TARGET_NAMESPACES")

if envVar == "" {
return []string{}
}

return strings.Split(envVar, ",")
}
36 changes: 22 additions & 14 deletions cyclops-ctrl/pkg/cluster/k8sclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@ import (
)

type KubernetesClient struct {
Dynamic dynamic.Interface
clientset *kubernetes.Clientset
discovery *discovery.DiscoveryClient
moduleset *client.CyclopsV1Alpha1Client
moduleNamespace string
helmReleaseNamespace string
Dynamic dynamic.Interface
clientset *kubernetes.Clientset
discovery *discovery.DiscoveryClient
moduleset *client.CyclopsV1Alpha1Client

moduleNamespace string
helmReleaseNamespace string
moduleTargetNamespaces []string

logger logr.Logger
}

func New(moduleNamespace, helmReleaseNamespace string, logger logr.Logger) (*KubernetesClient, error) {
func New(
moduleNamespace string,
helmReleaseNamespace string,
moduleTargetNamespaces []string,
logger logr.Logger,
) (*KubernetesClient, error) {
config := ctrl.GetConfigOrDie()
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
Expand All @@ -50,13 +57,14 @@ func New(moduleNamespace, helmReleaseNamespace string, logger logr.Logger) (*Kub
}

return &KubernetesClient{
Dynamic: dynamic,
discovery: discovery,
clientset: clientset,
moduleset: moduleSet,
moduleNamespace: moduleNamespace,
helmReleaseNamespace: helmReleaseNamespace,
logger: logger,
Dynamic: dynamic,
discovery: discovery,
clientset: clientset,
moduleset: moduleSet,
moduleNamespace: moduleNamespace,
helmReleaseNamespace: helmReleaseNamespace,
moduleTargetNamespaces: moduleTargetNamespaces,
logger: logger,
}, nil
}

Expand Down
4 changes: 4 additions & 0 deletions cyclops-ctrl/pkg/cluster/k8sclient/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ func (k *KubernetesClient) GetPodsForNode(nodeName string) ([]apiv1.Pod, error)
}

func (k *KubernetesClient) ListNamespaces() ([]string, error) {
if len(k.moduleTargetNamespaces) > 0 {
return k.moduleTargetNamespaces, nil
}

namespaceList, err := k.clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
Expand Down
3 changes: 0 additions & 3 deletions cyclops-ui/src/components/pages/NewModule/NewModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,6 @@ const NewModule = () => {
showSearch={true}
onChange={onTemplateStoreSelected}
style={{ width: "100%" }}
placeholder="default"
value="default"
defaultValue="default"
>
{namespaces.map((namespace: string) => (
<Option key={namespace} value={namespace}>
Expand Down

0 comments on commit 948e6de

Please sign in to comment.