Skip to content
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ab64f46
update getFiles to call generateProfileFiles when default profiles ex…
Nov 3, 2022
2a8695c
Merge branch 'main' into fix-profile-creation
Nov 3, 2022
6f6b3ef
linting
Nov 3, 2022
61c15f7
Merge branch 'main' into fix-profile-creation
Nov 3, 2022
e406323
Fixes test
foot Nov 4, 2022
750621f
Merge branch 'main' into fix-profile-creation
Nov 4, 2022
66a3d38
remove profile passing to test default annotation profiles
Nov 4, 2022
e38d607
Merge branch 'main' into fix-profile-creation
Nov 4, 2022
19c2ad4
Merge branch 'main' into fix-profile-creation
Nov 7, 2022
3a04805
fix conflicts.
Nov 9, 2022
dea6120
fix logic for iterating over profiles and add tests
Nov 9, 2022
8eae4dd
Merge branch 'main' into fix-profile-creation
Nov 10, 2022
f0c8af6
Simplify logic a bit
foot Nov 17, 2022
e879296
helmfakes.NewFakeChartCache supports Reader iface too now
foot Nov 17, 2022
f57a9b7
Merge branch 'main' into fix-profile-creation
Nov 17, 2022
b0ff049
Add GetLatestVersion and GetLayer
Nov 17, 2022
9bf1964
Merge branch 'main' into fix-profile-creation
Nov 17, 2022
a548d67
Fix args type issue
Nov 17, 2022
ba82942
Add helmfakes manually?
Nov 17, 2022
a78af4f
Fix arg type
Nov 17, 2022
5e93d13
Remove unused function
Nov 17, 2022
16c4683
Comment out default profile values test
Nov 17, 2022
50779c4
revert
Nov 17, 2022
2bfd345
Fix failing tests
Nov 17, 2022
dcf3f8e
Add layers tests.
Nov 17, 2022
b6933be
Refactor the generateProfileFiles a bit, pass in managementCluster
foot Nov 21, 2022
abcd30d
Add failing tests!
Nov 23, 2022
2d6c9cf
Fix test
Nov 23, 2022
abce9d0
Merge branch 'main' into fix-profile-creation
Nov 23, 2022
f2b2435
Fix conflicts
Nov 23, 2022
ac52b32
Actually pass through ProfileHelmRepository
foot Nov 23, 2022
3978356
Update cmd/clusters-service/pkg/server/clusters.go
Nov 23, 2022
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
7 changes: 4 additions & 3 deletions cmd/clusters-service/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/weaveworks/weave-gitops/pkg/kube"
core_server "github.com/weaveworks/weave-gitops/pkg/server"
"github.com/weaveworks/weave-gitops/pkg/server/auth"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -32,7 +33,7 @@ type Options struct {
ClusterFetcher clustersmngr.ClusterFetcher
GrpcRuntimeOptions []runtime.ServeMuxOption
RuntimeNamespace string
ProfileHelmRepository string
ProfileHelmRepository types.NamespacedName
HelmRepositoryCacheDirectory string
CAPIClustersNamespace string
CAPIEnabled bool
Expand Down Expand Up @@ -129,9 +130,9 @@ func WithRuntimeNamespace(RuntimeNamespace string) Option {
// WithProfileHelmRepository is used to set the name of the Flux
// HelmRepository object that will be inspected for Helm charts
// that include the profile annotation.
func WithProfileHelmRepository(name string) Option {
func WithProfileHelmRepository(repo types.NamespacedName) Option {
return func(o *Options) {
o.ProfileHelmRepository = name
o.ProfileHelmRepository = repo
}
}

Expand Down
35 changes: 18 additions & 17 deletions cmd/clusters-service/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
authv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
runtimeUtil "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/discovery"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -494,7 +495,7 @@ func StartServer(ctx context.Context, log logr.Logger, tempDir string, p Params)

return RunInProcessGateway(ctx, "0.0.0.0:8000",
WithLog(log),
WithProfileHelmRepository(p.HelmRepoName),
WithProfileHelmRepository(types.NamespacedName{Name: p.HelmRepoName, Namespace: p.HelmRepoNamespace}),
WithEntitlementSecretKey(client.ObjectKey{
Name: p.EntitlementSecretName,
Namespace: p.EntitlementSecretNamespace,
Expand Down Expand Up @@ -584,22 +585,22 @@ func RunInProcessGateway(ctx context.Context, addr string, setters ...Option) er
// Add weave-gitops enterprise handlers
clusterServer := server.NewClusterServer(
server.ServerOpts{
Logger: args.Log,
ClustersManager: args.CoreServerConfig.ClustersManager,
GitProvider: args.GitProvider,
ClientGetter: args.ClientGetter,
DiscoveryClient: args.DiscoveryClient,
ClustersNamespace: args.CAPIClustersNamespace,
ProfileHelmRepositoryName: args.ProfileHelmRepository,
HelmRepositoryCacheDir: args.HelmRepositoryCacheDirectory,
CAPIEnabled: args.CAPIEnabled,
ChartJobs: helm.NewJobs(),
ChartsCache: args.ChartsCache,
ValuesFetcher: helm.NewValuesFetcher(),
RestConfig: args.CoreServerConfig.RestCfg,
ManagementFetcher: args.ManagementFetcher,
Cluster: args.Cluster,
Estimator: estimator,
Logger: args.Log,
ClustersManager: args.CoreServerConfig.ClustersManager,
GitProvider: args.GitProvider,
ClientGetter: args.ClientGetter,
DiscoveryClient: args.DiscoveryClient,
ClustersNamespace: args.CAPIClustersNamespace,
ProfileHelmRepository: args.ProfileHelmRepository,
HelmRepositoryCacheDir: args.HelmRepositoryCacheDirectory,
CAPIEnabled: args.CAPIEnabled,
ChartJobs: helm.NewJobs(),
ChartsCache: args.ChartsCache,
ValuesFetcher: helm.NewValuesFetcher(),
RestConfig: args.CoreServerConfig.RestCfg,
ManagementFetcher: args.ManagementFetcher,
Cluster: args.Cluster,
Estimator: estimator,
},
)
if err := capi_proto.RegisterClustersServiceHandlerServer(ctx, grpcMux, clusterServer); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions cmd/clusters-service/pkg/server/automations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,6 @@ status: {}
clusterState: tt.clusterState,
namespace: "default",
provider: tt.provider,
hr: hr,
})

// request
Expand Down Expand Up @@ -809,7 +808,6 @@ status: {}
s := createServer(t, serverOptions{
clusterState: tt.clusterState,
namespace: "default",
hr: hr,
})

// request
Expand Down
124 changes: 24 additions & 100 deletions cmd/clusters-service/pkg/server/charts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package server

import (
"context"
"encoding/base64"
"errors"
"fmt"
"testing"
"time"

sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/google/go-cmp/cmp"
protos "github.com/weaveworks/weave-gitops-enterprise/cmd/clusters-service/pkg/protos"
"github.com/weaveworks/weave-gitops-enterprise/pkg/helm"
"github.com/weaveworks/weave-gitops-enterprise/pkg/helm/helmfakes"
"github.com/weaveworks/weave-gitops/core/clustersmngr/cluster"
"google.golang.org/protobuf/testing/protocmp"
"helm.sh/helm/v3/pkg/repo"
Expand All @@ -33,7 +31,7 @@ var defaultClusterState = []runtime.Object{
func TestListChartsForRepository(t *testing.T) {
testCases := []struct {
name string
fc *fakeChartCache
fc helm.ChartsCache
clusterState []runtime.Object
request *protos.ListChartsForRepositoryRequest
want *protos.ListChartsForRepositoryResponse
Expand All @@ -52,9 +50,9 @@ func TestListChartsForRepository(t *testing.T) {
Kind: "chart",
},
clusterState: defaultClusterState,
fc: newFakeChartCache(
cachedCharts(
clusterRefToString(
fc: helmfakes.NewFakeChartCache(
helmfakes.WithCharts(
helmfakes.ClusterRefToString(
helm.ObjectReference{Kind: "HelmRepository", Name: "bitnami-charts", Namespace: "demo"},
types.NamespacedName{Name: "management"},
), []helm.Chart{{Name: "redis", Version: "1.0.1", Kind: "chart"}, {Name: "postgres", Version: "1.0.2", Kind: "chart"}})),
Expand All @@ -79,9 +77,9 @@ func TestListChartsForRepository(t *testing.T) {
Kind: "chart",
},
clusterState: defaultClusterState,
fc: newFakeChartCache(
cachedCharts(
clusterRefToString(
fc: helmfakes.NewFakeChartCache(
helmfakes.WithCharts(
helmfakes.ClusterRefToString(
helm.ObjectReference{Kind: "HelmRepository", Name: "bitnami-charts", Namespace: "demo"},
types.NamespacedName{Name: "management"},
), []helm.Chart{{Name: "redis", Version: "1.0.1", Kind: "chart"}, {Name: "redis", Version: "1.0.2", Kind: "chart"}})),
Expand All @@ -105,9 +103,9 @@ func TestListChartsForRepository(t *testing.T) {
Kind: "chart",
},
clusterState: defaultClusterState,
fc: newFakeChartCache(
cachedCharts(
clusterRefToString(
fc: helmfakes.NewFakeChartCache(
helmfakes.WithCharts(
helmfakes.ClusterRefToString(
helm.ObjectReference{Kind: "HelmRepository", Name: "not-bitnami-charts", Namespace: "demo"},
types.NamespacedName{Name: "management"},
), []helm.Chart{{Name: "redis", Version: "1.0.1"}, {Name: "postgres", Version: "1.0.2"}})),
Expand All @@ -129,9 +127,9 @@ func TestListChartsForRepository(t *testing.T) {
Kind: "profile",
},
clusterState: defaultClusterState,
fc: newFakeChartCache(
cachedCharts(
clusterRefToString(
fc: helmfakes.NewFakeChartCache(
helmfakes.WithCharts(
helmfakes.ClusterRefToString(
helm.ObjectReference{Kind: "HelmRepository", Name: "bitnami-charts", Namespace: "demo"},
types.NamespacedName{Name: "management"},
),
Expand Down Expand Up @@ -171,7 +169,7 @@ func TestListChartsForRepository(t *testing.T) {
func TestGetValuesForChartFromValuesFetcher(t *testing.T) {
testCases := []struct {
name string
fc *fakeChartCache
fc helm.ChartsCacheReader
clusterState []runtime.Object
request *protos.GetValuesForChartRequest
want *protos.GetChartsJobResponse
Expand All @@ -191,9 +189,9 @@ func TestGetValuesForChartFromValuesFetcher(t *testing.T) {
Version: "1.0.1",
},
clusterState: defaultClusterState,
fc: newFakeChartCache(
cachedCharts(
clusterRefToString(
fc: helmfakes.NewFakeChartCache(
helmfakes.WithCharts(
helmfakes.ClusterRefToString(
helm.ObjectReference{Kind: "HelmRepository", Name: "bitnami-charts", Namespace: "demo"},
types.NamespacedName{Name: "management"},
),
Expand Down Expand Up @@ -261,7 +259,7 @@ func TestGetValuesForChartFromValuesFetcher(t *testing.T) {
func TestGetValuesForChartCached(t *testing.T) {
testCases := []struct {
name string
fc *fakeChartCache
fc helm.ChartsCacheReader
clusterState []runtime.Object
request *protos.GetValuesForChartRequest
want *protos.GetChartsJobResponse
Expand All @@ -281,15 +279,15 @@ func TestGetValuesForChartCached(t *testing.T) {
Version: "1.0.1",
},
clusterState: defaultClusterState,
fc: newFakeChartCache(
cachedCharts(
clusterRefToString(
fc: helmfakes.NewFakeChartCache(
helmfakes.WithCharts(
helmfakes.ClusterRefToString(
helm.ObjectReference{Kind: "HelmRepository", Name: "bitnami-charts", Namespace: "demo"},
types.NamespacedName{Name: "management"},
),
[]helm.Chart{{Name: "redis", Version: "1.0.1"}}),
cachedValues(
chartRefToString(
helmfakes.WithValues(
helmfakes.ChartRefToString(
helm.ObjectReference{Kind: "HelmRepository", Name: "bitnami-charts", Namespace: "demo"},
types.NamespacedName{Name: "management"},
helm.Chart{Name: "redis", Version: "1.0.1"}),
Expand Down Expand Up @@ -338,80 +336,6 @@ func TestGetValuesForChartCached(t *testing.T) {
}
}

func cachedCharts(key string, charts []helm.Chart) func(*fakeChartCache) {
return func(fc *fakeChartCache) {
fc.charts[key] = charts
}
}

func cachedValues(key string, chart []byte) func(*fakeChartCache) {
return func(fc *fakeChartCache) {
fc.chartValues[key] = []byte(base64.StdEncoding.EncodeToString(chart))
}
}

func newFakeChartCache(opts ...func(*fakeChartCache)) *fakeChartCache {
fc := &fakeChartCache{
charts: make(map[string][]helm.Chart),
chartValues: make(map[string][]byte),
}
for _, o := range opts {
o(fc)
}

return fc
}

type fakeChartCache struct {
charts map[string][]helm.Chart
chartValues map[string][]byte
}

func (fc fakeChartCache) ListChartsByRepositoryAndCluster(ctx context.Context, clusterRef types.NamespacedName, repoRef helm.ObjectReference, kind string) ([]helm.Chart, error) {
charts, ok := fc.charts[clusterRefToString(repoRef, clusterRef)]
if !ok {
return nil, errors.New("no charts found")
}
// filter by kind
var filtered []helm.Chart
for _, c := range charts {
if c.Kind == kind {
filtered = append(filtered, c)
}
}
return filtered, nil
}
func (fc fakeChartCache) IsKnownChart(ctx context.Context, clusterRef types.NamespacedName, repoRef helm.ObjectReference, chart helm.Chart) (bool, error) {
charts, ok := fc.charts[clusterRefToString(repoRef, clusterRef)]
if !ok {
return false, nil
}
for _, c := range charts {
if c.Name == chart.Name && c.Version == chart.Version {
return true, nil
}
}
return false, nil
}
func (fc fakeChartCache) GetChartValues(ctx context.Context, clusterRef types.NamespacedName, repoRef helm.ObjectReference, chart helm.Chart) ([]byte, error) {
if values, ok := fc.chartValues[chartRefToString(repoRef, clusterRef, chart)]; ok {
return values, nil
}
return nil, nil
}
func (fc fakeChartCache) UpdateValuesYaml(ctx context.Context, clusterRef types.NamespacedName, repoRef helm.ObjectReference, chart helm.Chart, valuesYaml []byte) error {
fc.chartValues[chartRefToString(repoRef, clusterRef, chart)] = valuesYaml
return nil
}

func clusterRefToString(or helm.ObjectReference, cr types.NamespacedName) string {
return fmt.Sprintf("%s_%s_%s_%s_%s", or.Kind, or.Name, or.Namespace, cr.Name, cr.Namespace)
}

func chartRefToString(or helm.ObjectReference, cr types.NamespacedName, c helm.Chart) string {
return fmt.Sprintf("%s_%s_%s_%s_%s_%s_%s", or.Kind, or.Name, or.Namespace, cr.Name, cr.Namespace, c.Name, c.Version)
}

type fakeValuesFetcher struct {
}

Expand Down
Loading