Skip to content

Commit

Permalink
fix(unleash): remove supports resource func
Browse files Browse the repository at this point in the history
Co-authored-by: starefossen <[email protected]>
  • Loading branch information
tommytroen and Starefossen committed May 15, 2024
1 parent 7f25024 commit 3559b50
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ local:
env bash -c 'source local.env; go run ./cmd/api'

debug:
env bash -c 'source local.env; dlv debug --headless --listen=:2345 --api-version=2 ./cmd/api
env bash -c 'source local.env; dlv debug --headless --listen=:2345 --api-version=2 ./cmd/api'

test:
go test -cover --race ./...
Expand Down
1 change: 0 additions & 1 deletion internal/k8s/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func parseResources(scheme *runtime.Scheme, dir fs.FS, path string) clusterResou

// This is a hack around how k8s unsafeGuesses resource plurals
func depluralized(s string) string {
fmt.Println(s)
switch s {
case "unleashs":
return "unleashes"
Expand Down
29 changes: 3 additions & 26 deletions internal/unleash/unleash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"strings"
"time"

"github.com/nais/api/internal/k8s"
Expand All @@ -14,7 +13,6 @@ import (
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -183,19 +181,17 @@ func createClient(apiServer, clusterName string, resources []schema.GroupVersion
return &k8sClient{
clientSet: clientSet,
dynamicClient: dynamicClient,
informers: createInformers(clientSet, dynamicClient, resources),
informers: createInformers(dynamicClient, resources),
}, nil
}

// @TODO: use namespace from config
func createInformers(clientSet kubernetes.Interface, dynamicClient dynamic.Interface, resources []schema.GroupVersionResource) []informers.GenericInformer {
func createInformers(dynamicClient dynamic.Interface, resources []schema.GroupVersionResource) []informers.GenericInformer {
dinf := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dynamicClient, 4*time.Hour, "bifrost-unleash", nil)

infs := make([]informers.GenericInformer, 0)
for _, resources := range resources {
if supportsResource(clientSet, resources) {
infs = append(infs, dinf.ForResource(resources))
}
infs = append(infs, dinf.ForResource(resources))
}
return infs
}
Expand All @@ -213,22 +209,3 @@ func hasSynced(ctx context.Context, cluster string, informer informers.GenericIn
}
return nil
}

func supportsResource(clientSet kubernetes.Interface, resource schema.GroupVersionResource) bool {
if clientSet, ok := clientSet.(*kubernetes.Clientset); ok {
resources, err := discovery.NewDiscoveryClient(clientSet.RESTClient()).ServerResourcesForGroupVersion(resource.GroupVersion().String())
if err != nil && !strings.Contains(err.Error(), "the server could not find the requested resource") {
logrus.Warnf("get server resources for group version: %v", err)
return false
}
if err == nil {
for _, r := range resources.APIResources {
if r.Name == resource.Resource {
return true
}
}
}
}
logrus.Warnf("resource %s not supported", resource.String())
return false
}

0 comments on commit 3559b50

Please sign in to comment.