Skip to content

Commit c7c5a4f

Browse files
authored
Support credential injection in AWSManagedCluster and AzureManagedCluster (#194)
* Adds support for injecting credentials into AWSManagedCluster and AzureManagedCluster * More loops
1 parent 8261470 commit c7c5a4f

File tree

2 files changed

+88
-97
lines changed

2 files changed

+88
-97
lines changed

cmd/capi-server/pkg/credentials/credentials.go

Lines changed: 54 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,36 @@ import (
2020
)
2121

2222
type IdentityParams struct {
23-
Group string
24-
Version string
25-
Kind string
26-
ClusterKind string
23+
Group string
24+
Versions []string
25+
Kind string
26+
ClusterKinds []string
2727
}
2828

2929
var IdentityParamsList = []IdentityParams{
30-
// v3
3130
{
32-
Group: "infrastructure.cluster.x-k8s.io",
33-
Version: "v1alpha3",
34-
Kind: "AWSClusterStaticIdentity",
35-
ClusterKind: "AWSCluster",
31+
Group: "infrastructure.cluster.x-k8s.io",
32+
Versions: []string{"v1alpha3", "v1alpha4"},
33+
Kind: "AWSClusterStaticIdentity",
34+
ClusterKinds: []string{"AWSCluster", "AWSManagedCluster"},
3635
},
3736
{
38-
Group: "infrastructure.cluster.x-k8s.io",
39-
Version: "v1alpha3",
40-
Kind: "AWSClusterRoleIdentity",
41-
ClusterKind: "AWSCluster",
37+
Group: "infrastructure.cluster.x-k8s.io",
38+
Versions: []string{"v1alpha3", "v1alpha4"},
39+
Kind: "AWSClusterRoleIdentity",
40+
ClusterKinds: []string{"AWSCluster", "AWSManagedCluster"},
4241
},
4342
{
44-
Group: "infrastructure.cluster.x-k8s.io",
45-
Version: "v1alpha3",
46-
Kind: "AzureClusterIdentity",
47-
ClusterKind: "AzureCluster",
43+
Group: "infrastructure.cluster.x-k8s.io",
44+
Versions: []string{"v1alpha3", "v1alpha4"},
45+
Kind: "AzureClusterIdentity",
46+
ClusterKinds: []string{"AzureCluster", "AzureManagedCluster"},
4847
},
4948
{
50-
Group: "infrastructure.cluster.x-k8s.io",
51-
Version: "v1alpha3",
52-
Kind: "VSphereClusterIdentity",
53-
ClusterKind: "VSphereCluster",
54-
},
55-
// v4
56-
{
57-
Group: "infrastructure.cluster.x-k8s.io",
58-
Version: "v1alpha4",
59-
Kind: "AWSClusterStaticIdentity",
60-
ClusterKind: "AWSCluster",
61-
},
62-
{
63-
Group: "infrastructure.cluster.x-k8s.io",
64-
Version: "v1alpha4",
65-
Kind: "AWSClusterRoleIdentity",
66-
ClusterKind: "AWSCluster",
67-
},
68-
{
69-
Group: "infrastructure.cluster.x-k8s.io",
70-
Version: "v1alpha4",
71-
Kind: "AzureClusterIdentity",
72-
ClusterKind: "AzureCluster",
73-
},
74-
{
75-
Group: "infrastructure.cluster.x-k8s.io",
76-
Version: "v1alpha4",
77-
Kind: "VSphereClusterIdentity",
78-
ClusterKind: "VSphereCluster",
49+
Group: "infrastructure.cluster.x-k8s.io",
50+
Versions: []string{"v1alpha3", "v1alpha4"},
51+
Kind: "VSphereClusterIdentity",
52+
ClusterKinds: []string{"VSphereCluster"},
7953
},
8054
}
8155

@@ -92,32 +66,34 @@ func isEmptyCredentials(creds *capiv1_proto.Credential) bool {
9266
func FindCredentials(ctx context.Context, c client.Client, dc discovery.DiscoveryInterface) ([]unstructured.Unstructured, error) {
9367
identities := []unstructured.Unstructured{}
9468
for _, identityParams := range IdentityParamsList {
95-
gvk := schema.GroupVersionKind{
96-
Group: identityParams.Group,
97-
Version: identityParams.Version,
98-
Kind: identityParams.Kind,
99-
}
69+
for _, v := range identityParams.Versions {
70+
gvk := schema.GroupVersionKind{
71+
Group: identityParams.Group,
72+
Version: v,
73+
Kind: identityParams.Kind,
74+
}
10075

101-
// We can skip this checkCRDExists check and let k8s do it.
102-
// BUT if any of the above Identities are missing, client-go will purge its
103-
// CRD cache and try and find all the available CRDs again, for each missing identity.
104-
// This is a lot of requests, they get throttled, this func blows out to 10s+.
105-
//
106-
exists, err := checkCRDExists(dc, gvk)
107-
if err != nil {
108-
return nil, fmt.Errorf("failed to check if CRD exists, %v: %w", gvk, err)
109-
}
110-
if !exists {
111-
continue
112-
}
76+
// We can skip this checkCRDExists check and let k8s do it.
77+
// BUT if any of the above Identities are missing, client-go will purge its
78+
// CRD cache and try and find all the available CRDs again, for each missing identity.
79+
// This is a lot of requests, they get throttled, this func blows out to 10s+.
80+
//
81+
exists, err := checkCRDExists(dc, gvk)
82+
if err != nil {
83+
return nil, fmt.Errorf("failed to check if CRD exists, %v: %w", gvk, err)
84+
}
85+
if !exists {
86+
continue
87+
}
11388

114-
identityList := &unstructured.UnstructuredList{}
115-
identityList.SetGroupVersionKind(gvk)
116-
err = c.List(context.Background(), identityList)
117-
if err != nil {
118-
return nil, fmt.Errorf("failed to list CRs of %v: %w", gvk, err)
89+
identityList := &unstructured.UnstructuredList{}
90+
identityList.SetGroupVersionKind(gvk)
91+
err = c.List(context.Background(), identityList)
92+
if err != nil {
93+
return nil, fmt.Errorf("failed to list CRs of %v: %w", gvk, err)
94+
}
95+
identities = append(identities, identityList.Items...)
11996
}
120-
identities = append(identities, identityList.Items...)
12197
}
12298

12399
// k8s doesn't internally differentiate between different apiVersions so we de-dup them
@@ -211,12 +187,15 @@ func InjectCredentials(tmplWithValues [][]byte, creds *capiv1_proto.Credential)
211187
for _, bit := range tmplWithValues {
212188
var err error
213189
for _, identityParams := range IdentityParamsList {
214-
// see if we can find the capi type in the list here.
215-
if creds.Group == identityParams.Group && creds.Kind == identityParams.Kind && creds.Version == identityParams.Version {
216-
clusterKind := identityParams.ClusterKind
217-
bit, err = MaybeInjectCredentials(bit, clusterKind, creds)
218-
if err != nil {
219-
return nil, fmt.Errorf("unable to inject credentials %v %v %v: %v", creds, bit, clusterKind, err)
190+
for _, v := range identityParams.Versions {
191+
// see if we can find the capi type in the list here.
192+
if creds.Group == identityParams.Group && creds.Kind == identityParams.Kind && creds.Version == v {
193+
for _, clusterKind := range identityParams.ClusterKinds {
194+
bit, err = MaybeInjectCredentials(bit, clusterKind, creds)
195+
if err != nil {
196+
return nil, fmt.Errorf("unable to inject credentials %v %v %v: %v", creds, bit, clusterKind, err)
197+
}
198+
}
220199
}
221200
}
222201
}

cmd/capi-server/pkg/credentials/credentials_test.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package credentials
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67

78
"github.com/google/go-cmp/cmp"
@@ -110,41 +111,52 @@ func TestInjectCredentials(t *testing.T) {
110111
t.Fatalf("result wasn't nil! %v", diff)
111112
}
112113

113-
var templateBits [][]byte
114-
templateBit := `
114+
templateBits := [][]byte{
115+
[]byte(`
115116
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha4
116117
kind: AWSCluster
117-
`
118-
templateBits = append(templateBits, []byte(templateBit))
118+
`),
119+
}
119120

120121
// no credentials
121122
result, _ = InjectCredentials(templateBits, nil)
122123
resultStr := convertToStringArray(result)
123-
if diff := cmp.Diff(resultStr[0], templateBit); diff != "" {
124+
if diff := cmp.Diff(resultStr[0], string(templateBits[0])); diff != "" {
124125
t.Fatalf("expected didn't match result! %v", diff)
125126
}
126127

127-
expected := `apiVersion: infrastructure.cluster.x-k8s.io/v1alpha4
128-
kind: AWSCluster
128+
for _, clusterKind := range []string{"AWSCluster", "AWSManagedCluster"} {
129+
t.Run(clusterKind, func(t *testing.T) {
130+
templateBits := [][]byte{
131+
[]byte(fmt.Sprintf(`
132+
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha4
133+
kind: %s
134+
`, clusterKind)),
135+
}
136+
137+
// with creds
138+
result, err := InjectCredentials(templateBits, &capiv1_protos.Credential{
139+
Group: "infrastructure.cluster.x-k8s.io",
140+
Version: "v1alpha4",
141+
Kind: "AWSClusterStaticIdentity",
142+
Name: "FooName",
143+
})
144+
if err != nil {
145+
t.Fatalf("unexpected err %v", err)
146+
}
147+
resultStr = convertToStringArray(result)
148+
149+
expected := fmt.Sprintf(`apiVersion: infrastructure.cluster.x-k8s.io/v1alpha4
150+
kind: %s
129151
spec:
130152
identityRef:
131153
kind: AWSClusterStaticIdentity
132154
name: FooName
133-
`
134-
// with creds
135-
result, err := InjectCredentials(templateBits, &capiv1_protos.Credential{
136-
Group: "infrastructure.cluster.x-k8s.io",
137-
Version: "v1alpha4",
138-
Kind: "AWSClusterStaticIdentity",
139-
Name: "FooName",
140-
})
141-
if err != nil {
142-
t.Fatalf("unexpected err %v", err)
143-
}
144-
resultStr = convertToStringArray(result)
145-
146-
if diff := cmp.Diff(resultStr[0], expected); diff != "" {
147-
t.Fatalf("expected didn't match result! %v", diff)
155+
`, clusterKind)
156+
if diff := cmp.Diff(resultStr[0], expected); diff != "" {
157+
t.Fatalf("expected didn't match result! %v", diff)
158+
}
159+
})
148160
}
149161
}
150162

0 commit comments

Comments
 (0)