Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback for misconfigured worker pool name case #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
docker-buildx: ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- $(CONTAINER_TOOL) buildx create --name kyma-workloads-webhook-builder
$(CONTAINER_TOOL) buildx use kyma-workloads-webhook-builder
- $(CONTAINER_TOOL) buildx create --name kim-snatch-builder
$(CONTAINER_TOOL) buildx use kim-snatch-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx rm kyma-workloads-webhook-builder
- $(CONTAINER_TOOL) buildx rm kim-snatch-builder
rm Dockerfile.cross

.PHONY: build-installer
Expand Down
4 changes: 2 additions & 2 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
domain: kyma-project.io
layout:
- go.kubebuilder.io/v4
projectName: kyma-workloads-webhook
repo: github.com/kyma-project/kyma-workloads-webhook
projectName: kim-snatch-webhook
repo: github.com/kyma-project/kim-snatch
resources:
- core: true
group: core
Expand Down
41 changes: 25 additions & 16 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import (
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.

corev1 "k8s.io/api/core/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/retry"

"github.com/kyma-project/kyma-workloads-webhook/internal/webhook/callback"
webhook "github.com/kyma-project/kyma-workloads-webhook/internal/webhook/server"
webhookcorev1 "github.com/kyma-project/kyma-workloads-webhook/internal/webhook/v1"
"github.com/kyma-project/kim-snatch/internal/webhook/callback"
webhook "github.com/kyma-project/kim-snatch/internal/webhook/server"
webhookcorev1 "github.com/kyma-project/kim-snatch/internal/webhook/v1"
admissionregistration "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -152,7 +153,7 @@ func main() {
logger.Error(err, "unable to read certificate")
os.Exit(1)
}
logger.Info("certificate loaded", certificateAuthorityName, string(data))
logger.Info("certificate loaded")

updateCABundle := callback.BuildUpdateCABundle(
context.Background(),
Expand Down Expand Up @@ -206,12 +207,26 @@ func main() {
os.Exit(1)
}

// nolint:goconst
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = webhookcorev1.SetupPodWebhookWithManager(mgr, kymaWorkerPoolName); err != nil {
logger.Error(err, "unable to create webhook", "webhook", "Pod")
os.Exit(1)
}
var nodeList corev1.NodeList
if err := rtClient.List(context.TODO(), &nodeList, client.MatchingLabels{
"worker.gardener.cloud/pool": kymaWorkerPoolName,
}); err != nil {
logger.Error(err, "unable to fetch node list")
os.Exit(1)
}

defaultPod := webhookcorev1.ApplyDefaults(kymaWorkerPoolName)
if len(nodeList.Items) == 0 {
errMsg := fmt.Sprintf("worker.gardener.cloud/pool=%s not exist, switching to fallback",
kymaWorkerPoolName)

logger.Error(errInvalidArgument, errMsg)
defaultPod = webhookcorev1.ApplyDefaultsFallback(kymaWorkerPoolName)
}

if err = webhookcorev1.SetupPodWebhookWithManager(mgr, defaultPod); err != nil {
logger.Error(err, "unable to create webhook", "webhook", "Pod")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

Expand All @@ -230,9 +245,3 @@ func main() {
os.Exit(1)
}
}

type MainOpts struct{}

func Main(opts MainOpts) error {
panic("not implemented yet")
}
2 changes: 1 addition & 1 deletion config/certmanager/issuer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: kyma
namespace: kyma-system
Expand Down
20 changes: 16 additions & 4 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace: kyma-system
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
namePrefix: snatch-
namePrefix: kim-snatch-

# Labels to add to all resources and selectors.
#labels:
Expand All @@ -16,7 +16,6 @@ namePrefix: snatch-

resources:
#- ../crd
- ../manager
- ../rbac
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand All @@ -34,6 +33,7 @@ resources:
# Only CR(s) which requires webhooks and are applied on namespaces labeled with 'webhooks: enabled' will
# be able to communicate with the Webhook Server.
#- ../network-policy
- ../manager

# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager
patches:
Expand All @@ -46,11 +46,23 @@ patches:
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
- path: manager_webhook_patch.yaml
- patch: |-
- op: add
path: /spec/template/spec/containers/0/args/-
value: --kyma-worker-pool-name=cpu-worker-0
target:
kind: Deployment
- patch: |-
- op: replace
path: /spec/template/spec/containers/0/imagePullPolicy
value: IfNotPresent
target:
kind: Deployment

replacements:
- source:
kind: Namespace
fieldPath: metadata.name
kind: Deployment
fieldPath: metadata.namespace
targets:
- select:
name: kyma
Expand Down
2 changes: 1 addition & 1 deletion config/default/manager_webhook_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: controller-manager
namespace: system
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
spec:
template:
Expand Down
3 changes: 2 additions & 1 deletion config/default/metrics_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Service
metadata:
labels:
control-plane: controller-manager
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: controller-manager-metrics-service
namespace: system
Expand All @@ -15,3 +15,4 @@ spec:
targetPort: 8443
selector:
control-plane: controller-manager
app.kubernetes.io/component: kim-snatch
5 changes: 3 additions & 2 deletions config/gardener/certmanager/certificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
labels:
app.kubernetes.io/created-by: kyma-workloads-webhook
app.kubernetes.io/part-of: kyma-workloads-webhook
app.kubernetes.io/created-by: kim-snatch
app.kubernetes.io/part-of: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: kyma
namespace: system
spec:
commonName: snatch-webhook-service.kyma-system
dnsNames:
- snatch-webhook-service.kyma-system.svc
- snatch-webhook-service.kyma-system.svc.cluster.local
isCA: true
issuerRef:
name: kyma
Expand Down
2 changes: 1 addition & 1 deletion config/gardener/certmanager/issuer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: cert.gardener.cloud/v1alpha1
kind: Issuer
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: kyma
namespace: system
Expand Down
8 changes: 7 additions & 1 deletion config/k3d/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ patches:
value: --kyma-worker-pool-name=snatch-test
target:
kind: Deployment
- patch: |-
- op: replace
path: /spec/template/spec/containers/0/imagePullPolicy
value: Never
target:
kind: Deployment

resources:
- ../manager
- ../rbac
- ../webhook
- metrics_service.yaml
- ../certmanager
- ../manager

sortOptions:
order: fifo
2 changes: 1 addition & 1 deletion config/k3d/manager_webhook_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: controller-manager
namespace: system
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
spec:
template:
Expand Down
2 changes: 1 addition & 1 deletion config/k3d/metrics_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Service
metadata:
labels:
control-plane: controller-manager
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: controller-manager-metrics-service
namespace: system
Expand Down
5 changes: 4 additions & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ metadata:
namespace: system
labels:
control-plane: controller-manager
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch-webhook
app.kubernetes.io/managed-by: kustomize
sidecar.istio.io/inject: "false"
spec:
selector:
matchLabels:
Expand All @@ -18,6 +19,8 @@ spec:
kubectl.kubernetes.io/default-container: manager
labels:
control-plane: controller-manager
app.kubernetes.io/component: kim-snatch
sidecar.istio.io/inject: "false"
spec:
# TODO(user): Uncomment the following code to configure the nodeAffinity expression
# according to the platforms which are supported by your solution.
Expand Down
2 changes: 1 addition & 1 deletion config/network-policy/allow-metrics-traffic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: allow-metrics-traffic
namespace: system
Expand Down
2 changes: 1 addition & 1 deletion config/network-policy/allow-webhook-traffic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: allow-webhook-traffic
namespace: system
Expand Down
2 changes: 1 addition & 1 deletion config/prometheus/monitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: ServiceMonitor
metadata:
labels:
control-plane: controller-manager
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: controller-manager-metrics-monitor
namespace: system
Expand Down
2 changes: 1 addition & 1 deletion config/rbac/leader_election_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: leader-election-role
rules:
Expand Down
2 changes: 1 addition & 1 deletion config/rbac/leader_election_role_binding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: leader-election-rolebinding
roleRef:
Expand Down
6 changes: 6 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ kind: ClusterRole
metadata:
name: manager-role
rules:
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- apiGroups:
- admissionregistration.k8s.io
resources:
Expand Down
2 changes: 1 addition & 1 deletion config/rbac/role_binding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: manager-rolebinding
roleRef:
Expand Down
2 changes: 1 addition & 1 deletion config/rbac/service_account.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: kim-snatch
app.kubernetes.io/managed-by: kustomize
name: controller-manager
namespace: system
2 changes: 1 addition & 1 deletion config/webhook/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ patches:
path: /webhooks/0/namespaceSelector
value:
matchLabels:
managed-by: kyma
operator.kyma-project.io/managed-by: kyma
target:
kind: MutatingWebhookConfiguration
3 changes: 2 additions & 1 deletion config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ webhooks:
namespace: system
path: /mutate--v1-pod
failurePolicy: Ignore
matchPolicy: Exact
name: mpod-v1.kb.io
reinvocationPolicy: IfNeeded
rules:
- apiGroups:
- ""
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- pods
sideEffects: None
3 changes: 2 additions & 1 deletion config/webhook/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/name: kyma-workloads-webhook
app.kubernetes.io/name: webhook-service
app.kubernetes.io/managed-by: kustomize
name: webhook-service
namespace: system
Expand All @@ -13,3 +13,4 @@ spec:
targetPort: 9443
selector:
control-plane: controller-manager
app.kubernetes.io/component: kim-snatch
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/kyma-project/kyma-workloads-webhook
module github.com/kyma-project/kim-snatch

go 1.23.0

Expand Down
2 changes: 1 addition & 1 deletion internal/webhook/callback/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/kyma-project/kyma-workloads-webhook/internal/webhook/callback"
"github.com/kyma-project/kim-snatch/internal/webhook/callback"
"github.com/stretchr/testify/assert"
admissionregistration "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
Loading
Loading