Skip to content

Commit 98bd6fb

Browse files
authored
Merge pull request #2 from kubernetes-sigs/master
Sync latest repo
2 parents 9eafc6e + 7177d6a commit 98bd6fb

38 files changed

+465
-400
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
!/api/**
66
!/cloud/**
77
!/controllers/**
8+
!/exp/**
89
!/pkg/**
910
!/main.go
1011
!/go.mod

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ KUBECTL=$(TOOLS_BIN_DIR)/kubectl
5050
KUSTOMIZE := $(abspath $(TOOLS_BIN_DIR)/kustomize)
5151
MOCKGEN := $(TOOLS_BIN_DIR)/mockgen
5252
RELEASE_NOTES := $(TOOLS_DIR)/$(RELEASE_NOTES_BIN)
53+
EXP_DIR := exp
5354

5455
# Define Docker related variables. Releases should modify and double check these vars.
5556
REGISTRY ?= gcr.io/$(shell gcloud config get-value project)
@@ -172,26 +173,28 @@ generate: ## Generate code
172173

173174
.PHONY: generate-go
174175
generate-go: $(CONTROLLER_GEN) $(MOCKGEN) $(CONVERSION_GEN) ## Runs Go related generate targets
175-
go generate ./...
176176
$(CONTROLLER_GEN) \
177177
paths=./api/... \
178+
paths=./$(EXP_DIR)/api/... \
178179
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt
179-
180180
$(CONVERSION_GEN) \
181181
--input-dirs=./api/v1alpha2 \
182182
--output-file-base=zz_generated.conversion \
183183
--go-header-file=./hack/boilerplate/boilerplate.generatego.txt
184+
go generate ./...
184185

185186
.PHONY: generate-manifests
186187
generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
187188
$(CONTROLLER_GEN) \
188189
paths=./api/... \
190+
paths=./$(EXP_DIR)/api/... \
189191
crd:crdVersions=v1 \
190192
output:crd:dir=$(CRD_ROOT) \
191193
output:webhook:dir=$(WEBHOOK_ROOT) \
192194
webhook
193195
$(CONTROLLER_GEN) \
194196
paths=./controllers/... \
197+
paths=./$(EXP_DIR)/controllers/... \
195198
output:rbac:dir=$(RBAC_ROOT) \
196199
rbac:roleName=manager-role
197200

@@ -338,7 +341,7 @@ create-management-cluster: $(KUSTOMIZE) $(ENVSUBST)
338341
@echo 'Set kubectl context to the kind management cluster by running "kubectl config set-context kind-capz"'
339342

340343
.PHONY: create-workload-cluster
341-
create-workload-cluster: $(KUSTOMIZE) $(ENVSUBST)
344+
create-workload-cluster: $(ENVSUBST)
342345
# Create workload Cluster.
343346
$(ENVSUBST) < $(TEMPLATES_DIR)/$(CLUSTER_TEMPLATE) | kubectl apply -f -
344347

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ hybrid deployments of Kubernetes.
1919

2020
Check out the [Cluster API Quick Start][quickstart] to create your first Kubernetes cluster on Azure using Cluster API.
2121

22-
## Features
23-
24-
TODO
25-
26-
---
22+
------
2723

2824
## Support Policy
2925

@@ -47,7 +43,7 @@ Each version of Cluster API for Azure will attempt to support at least two Kuber
4743

4844
**NOTE:** As the versioning for this project is tied to the versioning of Cluster API, future modifications to this policy may be made to more closely align with other providers in the Cluster API ecosystem.
4945

50-
---
46+
------
5147

5248
## Documentation
5349

api/v1alpha2/zz_generated.conversion.go

Lines changed: 0 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha3
18+
19+
import (
20+
"crypto/rand"
21+
"crypto/rsa"
22+
23+
"github.com/pkg/errors"
24+
"golang.org/x/crypto/ssh"
25+
)
26+
27+
// SetDefaultSSHPublicKey sets the default SSHPublicKey for an AzureMachine
28+
func (m *AzureMachine) SetDefaultSSHPublicKey() error {
29+
sshKeyData := m.Spec.SSHPublicKey
30+
if sshKeyData == "" {
31+
privateKey, perr := rsa.GenerateKey(rand.Reader, 2048)
32+
if perr != nil {
33+
return errors.Wrap(perr, "Failed to generate private key")
34+
}
35+
36+
publicRsaKey, perr := ssh.NewPublicKey(&privateKey.PublicKey)
37+
if perr != nil {
38+
return errors.Wrap(perr, "Failed to generate public key")
39+
}
40+
m.Spec.SSHPublicKey = string(ssh.MarshalAuthorizedKey(publicRsaKey))
41+
}
42+
43+
return nil
44+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha3
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
func TestAzureMachine_SetDefaultSSHPublicKey(t *testing.T) {
26+
g := NewWithT(t)
27+
28+
type test struct {
29+
machine *AzureMachine
30+
}
31+
32+
existingPublicKey := "testpublickey"
33+
publicKeyExistTest := test{machine: createMachineWithSSHPublicKey(t, existingPublicKey)}
34+
publicKeyNotExistTest := test{machine: createMachineWithSSHPublicKey(t, "")}
35+
36+
err := publicKeyExistTest.machine.SetDefaultSSHPublicKey()
37+
g.Expect(err).To(BeNil())
38+
g.Expect(publicKeyExistTest.machine.Spec.SSHPublicKey).To(Equal(existingPublicKey))
39+
40+
err = publicKeyNotExistTest.machine.SetDefaultSSHPublicKey()
41+
g.Expect(err).To(BeNil())
42+
g.Expect(publicKeyNotExistTest.machine.Spec.SSHPublicKey).To(Not(BeEmpty()))
43+
}
44+
45+
func createMachineWithSSHPublicKey(t *testing.T, sshPublicKey string) *AzureMachine {
46+
return &AzureMachine{
47+
Spec: AzureMachineSpec{
48+
SSHPublicKey: sshPublicKey,
49+
Image: &Image{
50+
SharedGallery: &AzureSharedGalleryImage{
51+
SubscriptionID: "SUB123",
52+
ResourceGroup: "RG123",
53+
Name: "NAME123",
54+
Gallery: "GALLERY1",
55+
Version: "1.0.0",
56+
},
57+
},
58+
},
59+
}
60+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha3
18+
19+
import (
20+
"golang.org/x/crypto/ssh"
21+
"k8s.io/apimachinery/pkg/util/validation/field"
22+
)
23+
24+
// ValidateSSHKey validates an SSHKey
25+
func ValidateSSHKey(sshKey string, fldPath *field.Path) field.ErrorList {
26+
allErrs := field.ErrorList{}
27+
28+
if _, _, _, _, err := ssh.ParseAuthorizedKey([]byte(sshKey)); err != nil {
29+
allErrs = append(allErrs, field.Required(fldPath, "the SSH public key is not valid"))
30+
return allErrs
31+
}
32+
33+
return allErrs
34+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha3
18+
19+
import (
20+
"crypto/rand"
21+
"crypto/rsa"
22+
"testing"
23+
24+
. "github.com/onsi/gomega"
25+
"golang.org/x/crypto/ssh"
26+
"k8s.io/apimachinery/pkg/util/validation/field"
27+
)
28+
29+
func TestAzureMachine_ValidateSSHKey(t *testing.T) {
30+
g := NewWithT(t)
31+
32+
tests := []struct {
33+
name string
34+
sshKey string
35+
wantErr bool
36+
}{
37+
{
38+
name: "valid ssh key",
39+
sshKey: generateSSHPublicKey(),
40+
wantErr: false,
41+
},
42+
{
43+
name: "invalid ssh key",
44+
sshKey: "invalid ssh key",
45+
wantErr: true,
46+
},
47+
}
48+
49+
for _, tc := range tests {
50+
t.Run(tc.name, func(t *testing.T) {
51+
err := ValidateSSHKey(tc.sshKey, field.NewPath("sshPublicKey"))
52+
if tc.wantErr {
53+
g.Expect(err).ToNot(HaveLen(0))
54+
} else {
55+
g.Expect(err).To(HaveLen(0))
56+
}
57+
})
58+
}
59+
}
60+
61+
func generateSSHPublicKey() string {
62+
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
63+
publicRsaKey, _ := ssh.NewPublicKey(&privateKey.PublicKey)
64+
return string(ssh.MarshalAuthorizedKey(publicRsaKey))
65+
}

api/v1alpha3/azuremachine_webhook.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,25 @@ func (m *AzureMachine) ValidateCreate() error {
4949
m.Name, errs)
5050
}
5151

52+
if errs := ValidateSSHKey(m.Spec.SSHPublicKey, field.NewPath("sshPublicKey")); len(errs) > 0 {
53+
return apierrors.NewInvalid(
54+
GroupVersion.WithKind("AzureMachine").GroupKind(),
55+
m.Name, errs)
56+
}
57+
5258
return nil
5359
}
5460

5561
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
5662
func (m *AzureMachine) ValidateUpdate(old runtime.Object) error {
5763
machinelog.Info("validate update", "name", m.Name)
5864

65+
if errs := ValidateSSHKey(m.Spec.SSHPublicKey, field.NewPath("sshPublicKey")); len(errs) > 0 {
66+
return apierrors.NewInvalid(
67+
GroupVersion.WithKind("AzureMachine").GroupKind(),
68+
m.Name, errs)
69+
}
70+
5971
return nil
6072
}
6173

@@ -65,3 +77,13 @@ func (m *AzureMachine) ValidateDelete() error {
6577

6678
return nil
6779
}
80+
81+
// Default implements webhookutil.defaulter so a webhook will be registered for the type
82+
func (m *AzureMachine) Default() {
83+
machinelog.Info("default", "name", m.Name)
84+
85+
err := m.SetDefaultSSHPublicKey()
86+
if err != nil {
87+
machinelog.Error(err, "SetDefaultSshPublicKey failed")
88+
}
89+
}

0 commit comments

Comments
 (0)