Skip to content

Commit 09cedaa

Browse files
authored
add dataplane webhooks and fix dp finalizer (#75)
1 parent d12e4a1 commit 09cedaa

File tree

7 files changed

+43
-19
lines changed

7 files changed

+43
-19
lines changed

operator/Makefile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ WITH_RELEASE_REPO = KO_DOCKER_REPO=$(RELEASE_REPO)
99
KIT_NAMESPACE ?= kit
1010
CONTAINER_IMAGE_REGISTRY ?=
1111
## Extra helm options
12-
HELM_OPTS ?=
12+
HELM_OPTS ?= --set serviceAccount.create=false
1313

1414
help: ## Display help
1515
@awk 'BEGIN {FS = ":.*##"; printf "Usage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@@ -47,14 +47,16 @@ licenses: ## Verifies dependency licenses and requires GITHUB_TOKEN to be set
4747
golicense hack/license-config.hcl bin/operator
4848

4949
apply: ## Apply the controller into your ~/.kube/config cluster
50-
KO_DOCKER_REPO=$(CONTAINER_IMAGE_REGISTRY)/kit ko apply --bare -f config/
51-
KO_DOCKER_REPO=$(CONTAINER_IMAGE_REGISTRY)/kit ko apply --bare -f config/controller
52-
KO_DOCKER_REPO=$(CONTAINER_IMAGE_REGISTRY)/kit ko apply --bare -f config/webhook
50+
KO_DOCKER_REPO=$(CONTAINER_IMAGE_REGISTRY)/kit helm template --include-crds kit charts/kit-operator --namespace $(KIT_NAMESPACE) \
51+
$(HELM_OPTS) \
52+
--set controller.image=ko://github.com/awslabs/kit/operator/cmd/controller \
53+
--set webhook.image=ko://github.com/awslabs/kit/operator/cmd/webhook \
54+
| $(WITH_GOFLAGS) ko apply -B -f -
5355

5456
delete: ## Delete the controller from your ~/.kube/config cluster
55-
kubectl delete -f config
56-
kubectl delete -f config/webhook
57-
kubectl delete -f config/controller
57+
helm template kit charts/kit-operator --namespace $(KIT_NAMESPACE) \
58+
$(HELM_OPTS) \
59+
| kubectl delete -f -
5860

5961
codegen: ## Generate code. Must be run if changes are made to ./pkg/apis/...
6062
hack/codegen.sh

operator/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ EOF
7171
2. Get the admin KUBECONFIG for the guest cluster from the substrate cluster
7272

7373
```bash
74-
kubectl get secret example-kube-admin-config -ojsonpath='{.data.config}' | base64 -d > /tmp/kubeconfig
74+
kubectl get secret ${GUEST_CLUSTER_NAME}-kube-admin-config -ojsonpath='{.data.config}' | base64 -d > /tmp/kubeconfig
7575
```
7676
> NOTE: It takes about 3-4 minutes for the cluster control plane to be available and healthy
7777
@@ -108,4 +108,4 @@ EOF
108108
--namespace kit \
109109
--cluster ${SUBSTRATE_CLUSTER_NAME} \
110110
--region=$AWS_REGION
111-
```
111+
```

operator/charts/kit-operator/templates/webhook/webhook.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: admissionregistration.k8s.io/v1
22
kind: MutatingWebhookConfiguration
33
metadata:
4-
name: defaulting.webhook.controlplane.kit.k8s.sh
4+
name: defaulting.webhook.kit.k8s.sh
55
webhooks:
66
- admissionReviewVersions: ["v1"]
77
clientConfig:
@@ -10,7 +10,7 @@ webhooks:
1010
namespace: {{ .Release.Namespace }}
1111
failurePolicy: Fail
1212
sideEffects: None
13-
name: defaulting.webhook.controlplane.kit.k8s.sh
13+
name: defaulting.webhook.kit.k8s.sh
1414
rules:
1515
- apiGroups:
1616
- kit.k8s.sh
@@ -19,6 +19,8 @@ webhooks:
1919
resources:
2020
- controlplanes
2121
controlplanes/status
22+
- dataplanes
23+
dataplanes/status
2224
operations:
2325
- CREATE
2426
- UPDATE
@@ -28,7 +30,7 @@ webhooks:
2830
apiVersion: admissionregistration.k8s.io/v1
2931
kind: ValidatingWebhookConfiguration
3032
metadata:
31-
name: validation.webhook.controlplane.kit.k8s.sh
33+
name: validation.webhook.kit.k8s.sh
3234
webhooks:
3335
- admissionReviewVersions: ["v1"]
3436
clientConfig:
@@ -37,7 +39,7 @@ webhooks:
3739
namespace: {{ .Release.Namespace }}
3840
failurePolicy: Fail
3941
sideEffects: None
40-
name: validation.webhook.controlplane.kit.k8s.sh
42+
name: validation.webhook.kit.k8s.sh
4143
rules:
4244
- apiGroups:
4345
- kit.k8s.sh
@@ -46,6 +48,8 @@ webhooks:
4648
resources:
4749
- controlplanes
4850
controlplanes/status
51+
- dataplanes
52+
dataplanes/status
4953
operations:
5054
- CREATE
5155
- UPDATE

operator/cmd/webhook/main.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import (
1818
"context"
1919
"flag"
2020

21-
"github.com/awslabs/kit/operator/pkg/apis/controlplane/v1alpha1"
21+
cpv1alpha1 "github.com/awslabs/kit/operator/pkg/apis/controlplane/v1alpha1"
22+
dpv1alpha1 "github.com/awslabs/kit/operator/pkg/apis/dataplane/v1alpha1"
23+
"k8s.io/apimachinery/pkg/runtime/schema"
2224

2325
"knative.dev/pkg/configmap"
2426
"knative.dev/pkg/controller"
@@ -28,12 +30,14 @@ import (
2830
"knative.dev/pkg/system"
2931
"knative.dev/pkg/webhook"
3032
"knative.dev/pkg/webhook/certificates"
33+
"knative.dev/pkg/webhook/resourcesemantics"
3134
"knative.dev/pkg/webhook/resourcesemantics/defaulting"
3235
"knative.dev/pkg/webhook/resourcesemantics/validation"
3336
)
3437

3538
var (
36-
options = Options{}
39+
options = Options{}
40+
kitResources = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{}
3741
)
3842

3943
type Options struct {
@@ -46,6 +50,12 @@ func main() {
4650

4751
config := injection.ParseAndGetRESTConfigOrDie()
4852

53+
// merge all kit resource handlers
54+
kitResources = cpv1alpha1.Resources
55+
for gvk, resource := range dpv1alpha1.Resources {
56+
kitResources[gvk] = resource
57+
}
58+
4959
// Controllers and webhook
5060
sharedmain.MainWithConfig(
5161
webhook.WithOptions(injection.WithNamespaceScope(signals.NewContext(), system.Namespace()), webhook.Options{
@@ -63,19 +73,19 @@ func main() {
6373

6474
func NewCRDDefaultingWebhook(ctx context.Context, w configmap.Watcher) *controller.Impl {
6575
return defaulting.NewAdmissionController(ctx,
66-
"defaulting.webhook.controlplane.kit.k8s.sh",
76+
"defaulting.webhook.kit.k8s.sh",
6777
"/default-resource",
68-
v1alpha1.Resources,
78+
kitResources,
6979
InjectContext,
7080
true,
7181
)
7282
}
7383

7484
func NewCRDValidationWebhook(ctx context.Context, w configmap.Watcher) *controller.Impl {
7585
return validation.NewAdmissionController(ctx,
76-
"validation.webhook.controlplane.kit.k8s.sh",
86+
"validation.webhook.kit.k8s.sh",
7787
"/validate-resource",
78-
v1alpha1.Resources,
88+
kitResources,
7989
InjectContext,
8090
true,
8191
)

operator/pkg/awsprovider/instances/reconciler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ func (c *Controller) Reconcile(ctx context.Context, dataplane *v1alpha1.DataPlan
6767
}
6868

6969
func (c *Controller) Finalize(ctx context.Context, dataplane *v1alpha1.DataPlane) error {
70+
asg, err := c.getAutoScalingGroup(ctx, AutoScalingGroupNameFor(dataplane))
71+
if err != nil {
72+
return err
73+
}
74+
// ASG is already gone
75+
if asg == nil {
76+
return nil
77+
}
7078
if _, err := c.autoscaling.DeleteAutoScalingGroupWithContext(ctx, &autoscaling.DeleteAutoScalingGroupInput{
7179
AutoScalingGroupName: ptr.String(AutoScalingGroupNameFor(dataplane)),
7280
ForceDelete: ptr.Bool(true), // terminate all the nodes in the ASG

0 commit comments

Comments
 (0)