Skip to content

Commit

Permalink
Merge branch 'main' into disable-reconciler
Browse files Browse the repository at this point in the history
# Conflicts:
#	cyclops-ctrl/cmd/main/main.go
  • Loading branch information
petar-cvit committed Nov 4, 2024
2 parents 36bfa69 + b078490 commit b12760b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 44 deletions.
12 changes: 8 additions & 4 deletions cyclops-ctrl/cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

cyclopsv1alpha1 "github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/auth"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/handler"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/integrations/helm"
Expand All @@ -31,6 +30,8 @@ import (
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/template/cache"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/template/render"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/cluster/k8sclient"

cyclopsv1alpha1 "github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
)

var (
Expand Down Expand Up @@ -71,7 +72,10 @@ func main() {
)
telemetryClient.InstanceStart()

k8sClient, err := k8sclient.New()
watchNamespace := getWatchNamespace()
helmWatchNamespace := getHelmWatchNamespace()

k8sClient, err := k8sclient.New(watchNamespace, helmWatchNamespace, zap.New(zap.UseFlagOptions(&opts)))
if err != nil {
fmt.Println("error bootstrapping Kubernetes client", err)
panic(err)
Expand All @@ -91,7 +95,7 @@ func main() {

prometheus.StartCacheMetricsUpdater(&monitor, templatesRepo.ReturnCache(), 10*time.Second, setupLog)

helmReleaseClient := helm.NewReleaseClient(getHelmWatchNamespace())
helmReleaseClient := helm.NewReleaseClient(helmWatchNamespace)

handler, err := handler.New(templatesRepo, k8sClient, helmReleaseClient, renderer, telemetryClient, monitor)
if err != nil {
Expand All @@ -117,7 +121,7 @@ func main() {
}),
Cache: ctrlCache.Options{
DefaultNamespaces: map[string]ctrlCache.Config{
getWatchNamespace(): {},
watchNamespace: {},
},
},
})
Expand Down
2 changes: 0 additions & 2 deletions cyclops-ctrl/internal/integrations/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"log"
"path"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -104,7 +103,6 @@ func (r *ReleaseClient) UpgradeRelease(
client.SetRegistryClient(registryClient)

for _, dependency := range current.Chart.Metadata.Dependencies {
fmt.Println(dependency.Repository, dependency.Name, path.Join(dependency.Repository, dependency.Name))
fp, err := client.LocateChart(dependency.Repository+"/"+dependency.Name, cli.New())
if err != nil {
return errors.Wrap(err, "failed to locate dependency chart")
Expand Down
35 changes: 16 additions & 19 deletions cyclops-ctrl/pkg/cluster/k8sclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package k8sclient
import (
"context"

"github.com/go-logr/logr"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -18,25 +19,18 @@ import (
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models/dto"
)

const (
cyclopsNamespace = "cyclops"
)

type KubernetesClient struct {
Dynamic dynamic.Interface

clientset *kubernetes.Clientset

discovery *discovery.DiscoveryClient

moduleset *client.CyclopsV1Alpha1Client
}
Dynamic dynamic.Interface
clientset *kubernetes.Clientset
discovery *discovery.DiscoveryClient
moduleset *client.CyclopsV1Alpha1Client
moduleNamespace string
helmReleaseNamespace string

func New() (*KubernetesClient, error) {
return createLocalClient()
logger logr.Logger
}

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

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

Expand Down
20 changes: 14 additions & 6 deletions cyclops-ctrl/pkg/cluster/k8sclient/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package k8sclient

import (
"context"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models/dto"
"sort"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"sort"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/models/dto"
)

func (k *KubernetesClient) GetResourcesForRelease(releaseName string) ([]dto.Resource, error) {
Expand Down Expand Up @@ -36,13 +39,18 @@ func (k *KubernetesClient) GetResourcesForRelease(releaseName string) ([]dto.Res

other := make([]unstructured.Unstructured, 0)
for _, gvr := range managedGVRs {
rs, err := k.Dynamic.Resource(gvr).List(context.Background(), metav1.ListOptions{
rs, err := k.Dynamic.Resource(gvr).Namespace(k.helmReleaseNamespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{
"app.kubernetes.io/instance": releaseName,
"app.kubernetes.io/managed-by": "Helm",
}).String(),
})
if err != nil {
if apierrors.IsNotFound(err) {
continue
}

k.logger.Error(err, "failed to list resources", "gvr", gvr, "namespace", k.helmReleaseNamespace)
continue
}

Expand Down Expand Up @@ -82,7 +90,7 @@ func (k *KubernetesClient) GetResourcesForRelease(releaseName string) ([]dto.Res
func (k *KubernetesClient) GetWorkloadsForRelease(releaseName string) ([]dto.Resource, error) {
out := make([]dto.Resource, 0, 0)

deployments, err := k.clientset.AppsV1().Deployments("").List(context.Background(), metav1.ListOptions{
deployments, err := k.clientset.AppsV1().Deployments(k.helmReleaseNamespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{
"app.kubernetes.io/instance": releaseName,
"app.kubernetes.io/managed-by": "Helm",
Expand All @@ -102,7 +110,7 @@ func (k *KubernetesClient) GetWorkloadsForRelease(releaseName string) ([]dto.Res
})
}

statefulset, err := k.clientset.AppsV1().StatefulSets("").List(context.Background(), metav1.ListOptions{
statefulset, err := k.clientset.AppsV1().StatefulSets(k.helmReleaseNamespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{
"app.kubernetes.io/instance": releaseName,
"app.kubernetes.io/managed-by": "Helm",
Expand All @@ -122,7 +130,7 @@ func (k *KubernetesClient) GetWorkloadsForRelease(releaseName string) ([]dto.Res
})
}

daemonsets, err := k.clientset.AppsV1().DaemonSets("").List(context.Background(), metav1.ListOptions{
daemonsets, err := k.clientset.AppsV1().DaemonSets(k.helmReleaseNamespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{
"app.kubernetes.io/instance": releaseName,
"app.kubernetes.io/managed-by": "Helm",
Expand Down
13 changes: 7 additions & 6 deletions cyclops-ctrl/pkg/cluster/k8sclient/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ const (
)

func (k *KubernetesClient) ListModules() ([]cyclopsv1alpha1.Module, error) {
moduleList, err := k.moduleset.Modules(cyclopsNamespace).List(metav1.ListOptions{})
moduleList, err := k.moduleset.Modules(k.moduleNamespace).List(metav1.ListOptions{})

return moduleList, err
}

func (k *KubernetesClient) CreateModule(module cyclopsv1alpha1.Module) error {
_, err := k.moduleset.Modules(cyclopsNamespace).Create(&module)
_, err := k.moduleset.Modules(k.moduleNamespace).Create(&module)
return err
}

func (k *KubernetesClient) UpdateModule(module *cyclopsv1alpha1.Module) error {
_, err := k.moduleset.Modules(cyclopsNamespace).Update(module)
_, err := k.moduleset.Modules(k.moduleNamespace).Update(module)
return err
}

func (k *KubernetesClient) UpdateModuleStatus(module *cyclopsv1alpha1.Module) (*cyclopsv1alpha1.Module, error) {
return k.moduleset.Modules(cyclopsNamespace).PatchStatus(module)
return k.moduleset.Modules(k.moduleNamespace).PatchStatus(module)
}

func (k *KubernetesClient) DeleteModule(name string) error {
return k.moduleset.Modules(cyclopsNamespace).Delete(name)
return k.moduleset.Modules(k.moduleNamespace).Delete(name)
}

func (k *KubernetesClient) GetModule(name string) (*cyclopsv1alpha1.Module, error) {
return k.moduleset.Modules(cyclopsNamespace).Get(name)
return k.moduleset.Modules(k.moduleNamespace).Get(name)
}

func (k *KubernetesClient) GetResourcesForModule(name string) ([]dto.Resource, error) {
Expand All @@ -71,6 +71,7 @@ func (k *KubernetesClient) GetResourcesForModule(name string) ([]dto.Resource, e
LabelSelector: "cyclops.module=" + name,
})
if err != nil {
k.logger.Error(err, "failed to list resources", "gvr", gvr, "namespace", k.helmReleaseNamespace)
continue
}

Expand Down
4 changes: 2 additions & 2 deletions cyclops-ctrl/pkg/cluster/k8sclient/templateauthrules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

func (k *KubernetesClient) ListTemplateAuthRules() ([]cyclopsv1alpha1.TemplateAuthRule, error) {
return k.moduleset.TemplateAuthRules(cyclopsNamespace).List(metav1.ListOptions{})
return k.moduleset.TemplateAuthRules(k.moduleNamespace).List(metav1.ListOptions{})
}

func (k *KubernetesClient) GetTemplateAuthRuleSecret(name, key string) (string, error) {
secret, err := k.clientset.CoreV1().Secrets(cyclopsNamespace).Get(context.Background(), name, metav1.GetOptions{})
secret, err := k.clientset.CoreV1().Secrets(k.moduleNamespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
return "", err
}
Expand Down
10 changes: 5 additions & 5 deletions cyclops-ctrl/pkg/cluster/k8sclient/templatestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import (
)

func (k *KubernetesClient) ListTemplateStore() ([]cyclopsv1alpha1.TemplateStore, error) {
return k.moduleset.TemplateStore(cyclopsNamespace).List(metav1.ListOptions{})
return k.moduleset.TemplateStore(k.moduleNamespace).List(metav1.ListOptions{})
}

func (k *KubernetesClient) CreateTemplateStore(ts *cyclopsv1alpha1.TemplateStore) error {
_, err := k.moduleset.TemplateStore(cyclopsNamespace).Create(ts)
_, err := k.moduleset.TemplateStore(k.moduleNamespace).Create(ts)
return err
}

func (k *KubernetesClient) UpdateTemplateStore(ts *cyclopsv1alpha1.TemplateStore) error {
curr, err := k.moduleset.TemplateStore(cyclopsNamespace).Get(ts.Name)
curr, err := k.moduleset.TemplateStore(k.moduleNamespace).Get(ts.Name)
if err != nil {
return err
}

ts.SetResourceVersion(curr.GetResourceVersion())

_, err = k.moduleset.TemplateStore(cyclopsNamespace).Update(ts)
_, err = k.moduleset.TemplateStore(k.moduleNamespace).Update(ts)
return err
}

func (k *KubernetesClient) DeleteTemplateStore(name string) error {
return k.moduleset.TemplateStore(cyclopsNamespace).Delete(name)
return k.moduleset.TemplateStore(k.moduleNamespace).Delete(name)
}

0 comments on commit b12760b

Please sign in to comment.