Skip to content

Commit

Permalink
feat: Support multiple registry credentials if specified
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmidyson committed Feb 12, 2024
1 parent 0511fb4 commit 0a01938
Show file tree
Hide file tree
Showing 21 changed files with 413 additions and 303 deletions.
1 change: 0 additions & 1 deletion api/v1alpha1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ func (ImageRegistries) VariableSchema() clusterv1.VariableSchema {
Description: "Configuration for image registries.",
Type: "array",
Items: ptr.To(ImageRegistry{}.VariableSchema().OpenAPIV3Schema),
MaxItems: ptr.To[int64](1),
},
}
}
Expand Down
4 changes: 3 additions & 1 deletion common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go 1.21
replace github.com/d2iq-labs/capi-runtime-extensions/api => ../api

require (
github.com/avast/retry-go/v4 v4.5.1
github.com/d2iq-labs/capi-runtime-extensions/api v0.0.0-00010101000000-000000000000
github.com/evanphx/json-patch/v5 v5.9.0
github.com/go-logr/logr v1.4.1
Expand All @@ -18,6 +19,7 @@ require (
k8s.io/api v0.29.1
k8s.io/apiextensions-apiserver v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/cluster-api v1.6.1
sigs.k8s.io/controller-runtime v0.17.0
Expand All @@ -31,6 +33,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand Down Expand Up @@ -74,7 +77,6 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiserver v0.29.1 // indirect
k8s.io/client-go v0.29.1 // indirect
k8s.io/cluster-bootstrap v0.29.1 // indirect
k8s.io/component-base v0.29.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions common/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 h1:4daAzAu0S6Vi7/lbWECcX0j45yZReDZ56BQsrVBOEEY=
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh7o=
github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
Expand Down
59 changes: 59 additions & 0 deletions common/pkg/k8s/client/fakessa/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package fakessa

import (
"context"
"errors"
"fmt"

"github.com/avast/retry-go/v4"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
)

func NewClient(_ *rest.Config, _ client.Options) (client.Client, error) {
return fake.NewClientBuilder().WithInterceptorFuncs(
interceptor.Funcs{
Patch: func(
ctx context.Context,
clnt client.WithWatch,
obj client.Object,
patch client.Patch,
opts ...client.PatchOption,
) error {
// Apply patches are supposed to upsert, but fake client fails if the object doesn't exist,
// if an apply patch occurs for an object that doesn't yet exist, create it.
if patch.Type() != types.ApplyPatchType {
return clnt.Patch(ctx, obj, patch, opts...)
}
check, ok := obj.DeepCopyObject().(client.Object)
if !ok {
return errors.New("could not check for object in fake client")
}

return retry.Do(
func() error {
if err := clnt.Get(ctx, client.ObjectKeyFromObject(obj), check); k8serrors.IsNotFound(
err,
) {
if err := clnt.Create(ctx, check); err != nil {
return fmt.Errorf(
"could not inject object creation for fake: %w",
err,
)
}
}
return clnt.Patch(ctx, obj, patch, opts...)
},
retry.Attempts(5),
)
},
},
).Build(), nil
}
14 changes: 13 additions & 1 deletion common/pkg/testutils/capitest/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
gomegatypes "github.com/onsi/gomega/types"
"gomodules.xyz/jsonpatch/v2"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"

"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest/request"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest/serializer"
)

Expand Down Expand Up @@ -51,7 +53,17 @@ func ValidateGeneratePatches[T mutation.GeneratePatches](
h := handlerCreator()
req := &runtimehooksv1.GeneratePatchesRequest{
Variables: tt.Vars,
Items: []runtimehooksv1.GeneratePatchesRequestItem{tt.RequestItem},
Items: []runtimehooksv1.GeneratePatchesRequestItem{
tt.RequestItem,
{
HolderReference: runtimehooksv1.HolderReference{
APIVersion: capiv1.GroupVersion.String(),
Kind: "Cluster",
Namespace: request.Namespace,
Name: request.ClusterName,
},
},
},
}
resp := &runtimehooksv1.GeneratePatchesResponse{}
h.GeneratePatches(context.Background(), req, resp)
Expand Down
16 changes: 10 additions & 6 deletions common/pkg/testutils/capitest/request/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ func NewKubeadmControlPlaneTemplateRequest(
},
},
&runtimehooksv1.HolderReference{
Kind: "Cluster",
FieldPath: "spec.controlPlaneRef",
Name: ClusterName,
Namespace: Namespace,
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
FieldPath: "spec.controlPlaneRef",
Name: ClusterName,
Namespace: Namespace,
},
uid,
)
Expand Down Expand Up @@ -152,8 +153,11 @@ func NewAWSClusterTemplateRequestItem(
return NewRequestItem(
awsClusterTemplate,
&runtimehooksv1.HolderReference{
Kind: "Cluster",
FieldPath: "spec.infrastructureRef",
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
FieldPath: "spec.infrastructureRef",
Name: ClusterName,
Namespace: Namespace,
},
uid,
)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/avast/retry-go/v4 v4.5.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 h1:4daAzAu0S6Vi7/lbWECcX0j45yZReDZ56BQsrVBOEEY=
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh7o=
github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
Expand Down
7 changes: 2 additions & 5 deletions pkg/handlers/aws/mutation/metapatch_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"testing"

"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/k8s/client/fakessa"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/ami"
amitests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/ami/tests"
calicotests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/cni/calico/tests"
Expand Down Expand Up @@ -61,9 +60,7 @@ func TestGeneratePatches(t *testing.T) {
mgr, _ := manager.New(
&rest.Config{},
manager.Options{
NewClient: func(_ *rest.Config, _ client.Options) (client.Client, error) {
return fake.NewClientBuilder().Build(), nil
},
NewClient: fakessa.NewClient,
},
)

Expand Down
7 changes: 2 additions & 5 deletions pkg/handlers/docker/mutation/metapatch_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"testing"

"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/k8s/client/fakessa"
dockerclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/mutation/customimage"
customimagetests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/mutation/customimage/tests"
Expand Down Expand Up @@ -51,9 +50,7 @@ func TestGeneratePatches(t *testing.T) {
mgr, _ := manager.New(
&rest.Config{},
manager.Options{
NewClient: func(_ *rest.Config, _ client.Options) (client.Client, error) {
return fake.NewClientBuilder().Build(), nil
},
NewClient: fakessa.NewClient,
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
systemdProxyTemplateContents string

systemdProxyTemplate *template.Template = template.Must(
template.New("systemd.conf.tmpl").Parse(systemdProxyTemplateContents),
template.New("").Parse(systemdProxyTemplateContents),
)

systemdUnitPaths = []string{
Expand Down
Loading

0 comments on commit 0a01938

Please sign in to comment.