Skip to content

Commit

Permalink
feat: add kimup admission controller operator (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod authored Oct 15, 2024
1 parent 129ab6a commit ef9fda6
Show file tree
Hide file tree
Showing 21 changed files with 963 additions and 440 deletions.
54 changes: 27 additions & 27 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_name: kube-image-updater
project_name: kimup
version: 2
before:
hooks:
Expand All @@ -9,7 +9,7 @@ release:
prerelease: auto

snapshot:
name_template: "{{ .Tag }}"
version_template: "{{ .Tag }}"

checksum:
name_template: '{{ .ProjectName }}-{{ .Version }}-checksums.txt'
Expand Down Expand Up @@ -37,9 +37,9 @@ builds:
- arm64
env:
- CGO_ENABLED=0
- id: "kimup-webhook"
binary: kimup-webhook
main: ./cmd/webhook
- id: "kimup-admission-controller"
binary: kimup-admission-controller
main: ./cmd/admission-controller
goos:
- linux
- darwin
Expand Down Expand Up @@ -128,41 +128,41 @@ dockers:
- --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }}
- --label=org.opencontainers.image.revision={{ .FullCommit }}

# * KIMUP-WEBHOOK
# * KIMUP-ADMISSION-CONTROLLER
- goarch: amd64
image_templates:
- "ghcr.io/orange-cloudavenue/{{.ProjectName}}-webhook:{{ .Version }}-amd64"
- "ghcr.io/orange-cloudavenue/{{.ProjectName}}-admission-controller:{{ .Version }}-amd64"
dockerfile: Dockerfile
use: buildx
ids:
- kimup-webhook
- kimup-admission-controller
build_flag_templates:
- --platform=linux/amd64
- "--build-arg=BINNAME=kimup-webhook"
- "--build-arg=BINNAME=kimup-admission-controller"
- --pull
- --label=org.opencontainers.image.title="kimup-webhook"
- --label=org.opencontainers.image.description="kube-image-updater-webhook"
- --label=org.opencontainers.image.url=https://github.com/orange-cloudavenue/{{ .ProjectName }}-webhook
- --label=org.opencontainers.image.source=https://github.com/orange-cloudavenue/{{ .ProjectName }}-webhook
- --label=org.opencontainers.image.title="kimup-admission-controller"
- --label=org.opencontainers.image.description="kube-image-updater-admission-controller"
- --label=org.opencontainers.image.url=https://github.com/orange-cloudavenue/{{ .ProjectName }}-admission-controller
- --label=org.opencontainers.image.source=https://github.com/orange-cloudavenue/{{ .ProjectName }}-admission-controller
- --label=org.opencontainers.image.version={{ .Version }}
- --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }}
- --label=org.opencontainers.image.revision={{ .FullCommit }}

- goarch: arm64
image_templates:
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-webhook:{{ .Version }}-arm64v8"
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-admission-controller:{{ .Version }}-arm64v8"
dockerfile: Dockerfile
use: buildx
ids:
- kimup-webhook
- kimup-admission-controller
build_flag_templates:
- --platform=linux/arm64/v8
- "--build-arg=BINNAME=kimup-webhook"
- "--build-arg=BINNAME=kimup-admission-controller"
- --pull
- --label=org.opencontainers.image.title="kimup-webhook"
- --label=org.opencontainers.image.description="kube-image-updater-webhook"
- --label=org.opencontainers.image.url=https://github.com/orange-cloudavenue/{{ .ProjectName }}-webhook
- --label=org.opencontainers.image.source=https://github.com/orange-cloudavenue/{{ .ProjectName }}-webhook
- --label=org.opencontainers.image.title="kimup-admission-controller"
- --label=org.opencontainers.image.description="kube-image-updater-admission-controller"
- --label=org.opencontainers.image.url=https://github.com/orange-cloudavenue/{{ .ProjectName }}-admission-controller
- --label=org.opencontainers.image.source=https://github.com/orange-cloudavenue/{{ .ProjectName }}-admission-controller
- --label=org.opencontainers.image.version={{ .Version }}
- --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }}
- --label=org.opencontainers.image.revision={{ .FullCommit }}
Expand All @@ -188,12 +188,12 @@ docker_manifests:
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-operator:v{{ .Version }}-amd64"
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-operator:v{{ .Version }}-arm64v8"

# * KIMUP-WEBHOOK
- name_template: "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-webhook:v{{ .Version }}"
# * KIMUP-ADMISSION-CONTROLLER
- name_template: "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-admission-controller:v{{ .Version }}"
image_templates:
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-webhook:v{{ .Version }}-amd64"
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-webhook:v{{ .Version }}-arm64v8"
- name_template: "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-webhook:latest"
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-admission-controller:v{{ .Version }}-amd64"
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-admission-controller:v{{ .Version }}-arm64v8"
- name_template: "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-admission-controller:latest"
image_templates:
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-webhook:v{{ .Version }}-amd64"
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-webhook:v{{ .Version }}-arm64v8"
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-admission-controller:v{{ .Version }}-amd64"
- "ghcr.io/orange-cloudavenue/{{ .ProjectName }}-admission-controller:v{{ .Version }}-arm64v8"
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/operator cmd/operator/main.go
go build -o bin/kimup cmd/kimup/*
go build -o bin/webhook cmd/webhook/*
go build -o bin/admission-controller cmd/admission-controller/*

.PHONY: build
build-webhook: manifests generate fmt vet
go build -o bin/webhook cmd/webhook/*
build-admission-controller: manifests generate fmt vet
go build -o bin/admission-controller cmd/admission-controller/*

.PHONY: build-kimup
build-kimup: manifests generate fmt vet
Expand All @@ -100,9 +100,9 @@ run-operator: manifests generate fmt vet ## Run a controller from your host.
run-kimup: manifests generate fmt vet ## Run the image updater from your host.
go run ./cmd/kimup

.PHONY: run-webhook
run-webhook: manifests generate fmt vet ## Run the webhook from your host.
go run ./cmd/webhook/
.PHONY: run-admission-controller
run-admission-controller: manifests generate fmt vet ## Run the admission-controller from your host.
go run ./cmd/admission-controller/

.PHONY: run-mkdocs
run-mkdocs: ## Run mkdocs to serve the documentation locally.
Expand Down
19 changes: 11 additions & 8 deletions api/v1alpha1/kimup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import (
type (
// KimupSpec defines the desired state of Kimup
KimupSpec struct {
// TODO add namespace and serviceaccount settings

// +kubebuilder:validation:Optional
Controller *KimupControllerSpec `json:"controller"`

// +kubebuilder:validation:Optional
Webhook *KimupWebhookSpec `json:"webhook"`
AdmissionController *KimupAdmissionControllerSpec `json:"admissionController"`
}

// ! Controller
Expand All @@ -44,17 +46,17 @@ type (
// Service *KimupServiceSpec `json:"service,omitempty"`
}

// ! Webhook
// ! AdmissionController

KimupWebhookSpec struct {
KimupAdmissionControllerSpec struct {
// +kubebuilder:validation:Optional
// +kubebuilder:default:=Deployment
// +kubebuilder:validation:Enum=Deployment;DaemonSet
DeploymentType string `json:"deploymentType,omitempty"`

// +kubebuilder:validation:Optional
// +kubebuilder:default:=3
// +kubebuilder:description: Number of replicas for the webhook deployment. (Only for Deployment)
// +kubebuilder:description: Number of replicas (default: 3) for the admissionController deployment. (Only for Deployment)
Replicas int32 `json:"replicas,omitempty"`

KimupInstanceSpec `json:",inline"`
Expand Down Expand Up @@ -135,6 +137,7 @@ type (

// +kubebuilder:validation:Optional
// +kubebuilder:description: Service account name for the Kimup pods.
// +kubebuilder:default:=kimup
ServiceAccountName string `json:"serviceAccountName,omitempty"`

// +kubebuilder:validation:Optional
Expand Down Expand Up @@ -170,15 +173,15 @@ type (
KimupStatus struct {
Controller KimupInstanceStatus `json:"controller,omitempty"`

Webhook KimupInstanceStatus `json:"webhook,omitempty"`
AdmissionController KimupInstanceStatus `json:"admissionController,omitempty"`
}

KimupInstanceStatus struct {
// Status of the Kimup Instance
// It can be one of the following:
// - "ready": The kimup instance is ready to serve requests
// - "resources-created": The Kimup instance resources were created but not yet configured
Phase string `json:"phase,omitempty"`
State string `json:"state,omitempty"`

// IsRollingUpdate is true if the kimup instance is being updated
IsRollingUpdate bool `json:"isRollingUpdate,omitempty"`
Expand All @@ -189,8 +192,8 @@ type (
// +kubebuilder:subresource:status

// Kimup is the Schema for the kimups API
// +kubebuilder:printcolumn:name="Controller",type=string,JSONPath=`.status.controller.phase`
// +kubebuilder:printcolumn:name="Webhook",type=string,JSONPath=`.status.webhook.phase`
// +kubebuilder:printcolumn:name="Controller",type=string,JSONPath=`.status.controller.state`
// +kubebuilder:printcolumn:name="AdmissionController",type=string,JSONPath=`.status.admissionController.state`
type Kimup struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
42 changes: 21 additions & 21 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
10 changes: 3 additions & 7 deletions cmd/webhook/main.go → cmd/admission-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,10 @@ func main() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)

// homedir for kubeconfig
homedir, err := os.UserHomeDir()
// kubernetes golang library provide flag "kubeconfig" to specify the path to the kubeconfig file
kubeClient, err = client.New(flag.Lookup("kubeconfig").Value.String())
if err != nil {
panic(err)
}
kubeClient, err = client.New(homedir + "/.kube/config")
if err != nil {
panic(err)
log.Panicf("Error creating kubeclient: %v", err)
}

// * Webhook server
Expand Down
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions cmd/kimup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
"net"
"os"
"os/signal"
"syscall"
Expand All @@ -11,7 +12,9 @@ import (
log "github.com/sirupsen/logrus"

"github.com/orange-cloudavenue/kube-image-updater/internal/annotations"
"github.com/orange-cloudavenue/kube-image-updater/internal/httpserver"
"github.com/orange-cloudavenue/kube-image-updater/internal/kubeclient"
"github.com/orange-cloudavenue/kube-image-updater/internal/models"
"github.com/orange-cloudavenue/kube-image-updater/internal/triggers"
"github.com/orange-cloudavenue/kube-image-updater/internal/utils"
)
Expand Down Expand Up @@ -45,6 +48,23 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// * Config the metrics and healthz server
a, waitHTTP := httpserver.Init(ctx, httpserver.WithCustomHandlerForHealth(
func() (bool, error) {
// TODO improve
_, err := net.DialTimeout("tcp", models.HealthzDefaultAddr, 5*time.Second)
if err != nil {
return false, err
}
return true, nil
}))

if err := a.Run(); err != nil {
log.Errorf("Failed to start HTTP servers: %v", err)
// send signal to stop the program
c <- syscall.SIGINT
}

initScheduler(ctx, k)

go func() {
Expand Down Expand Up @@ -117,4 +137,5 @@ func main() {

<-c
cancel()
waitHTTP()
}
Loading

0 comments on commit ef9fda6

Please sign in to comment.