Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespaced resource fetching #671

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cyclops-ctrl/cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ func main() {
telemetryClient.InstanceStart()

watchNamespace := getWatchNamespace()
helmWatchNamespace := getHelmWatchNamespace()

k8sClient, err := k8sclient.New(watchNamespace)
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 @@ -93,7 +94,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 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
28 changes: 17 additions & 11 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 @@ -19,14 +20,17 @@ import (
)

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

logger logr.Logger
}

func New(moduleNamespace string) (*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 @@ -46,11 +50,13 @@ func New(moduleNamespace string) (*KubernetesClient, error) {
}

return &KubernetesClient{
Dynamic: dynamic,
discovery: discovery,
clientset: clientset,
moduleset: moduleSet,
moduleNamespace: moduleNamespace,
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
1 change: 1 addition & 0 deletions cyclops-ctrl/pkg/cluster/k8sclient/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading