From 3f628ef341f6aac4807df334c462580c7e508f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kris=20N=C3=B3va?= Date: Mon, 1 Nov 2021 15:26:34 +0000 Subject: [PATCH] adding ingressclass and validatingwebhookconfiguration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kris Nóva --- Makefile | 2 +- codify.go | 6 ++ codify/ingress.go | 2 +- codify/ingressclass.go | 91 +++++++++++++++++++++++ codify/validatingwebhookconfiguration.go | 92 ++++++++++++++++++++++++ 5 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 codify/ingressclass.go create mode 100644 codify/validatingwebhookconfiguration.go diff --git a/Makefile b/Makefile index 44d4118..9339601 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ version=$(shell git rev-parse HEAD) # Global release version. # Change this to bump the build version! -version="0.3.1" +version="0.3.2" compile: ## Compile for the local architecture ⚙ @echo "Compiling..." diff --git a/codify.go b/codify.go index 680bacb..0e22de4 100644 --- a/codify.go +++ b/codify.go @@ -33,6 +33,8 @@ import ( "strings" "text/template" + admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + "k8s.io/apimachinery/pkg/runtime" "github.com/fatih/color" @@ -316,8 +318,12 @@ func toCodify(raw []byte) ([]CodifyObject, error) { objects = append(objects, codify.NewServiceAccount(x)) case *corev1.Secret: objects = append(objects, codify.NewSecret(x)) + case *networkingv1.IngressClass: + objects = append(objects, codify.NewIngressClass(x)) case *networkingv1.Ingress: objects = append(objects, codify.NewIngress(x)) + case *admissionregistrationv1.ValidatingWebhookConfiguration: + objects = append(objects, codify.NewValidatingwebhookConfiguration(x)) // CRDs is going to take some special care... //case *apiextensionsv1.CustomResourceDefinition: // objects = append(objects, codify.NewCustomResourceDefinition(x)) diff --git a/codify/ingress.go b/codify/ingress.go index 04738ad..bf4463c 100644 --- a/codify/ingress.go +++ b/codify/ingress.go @@ -74,7 +74,7 @@ func (k Ingress) Install() (string, []string) { func (k Ingress) Uninstall() string { uninstall := ` if client != nil { - err = client.NetworkingV1().Ingresss("{{ .KubeObject.Namespace }}").Delete(context.TODO(), "{{ .KubeObject.Name }}", metav1.DeleteOptions{}) + err = client.NetworkingV1().Ingress("{{ .KubeObject.Namespace }}").Delete(context.TODO(), "{{ .KubeObject.Name }}", metav1.DeleteOptions{}) if err != nil { return err } diff --git a/codify/ingressclass.go b/codify/ingressclass.go new file mode 100644 index 0000000..be2cb8f --- /dev/null +++ b/codify/ingressclass.go @@ -0,0 +1,91 @@ +// +// Copyright © 2021 Kris Nóva +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ███╗ ██╗ █████╗ ███╗ ███╗██╗ +// ████╗ ██║██╔══██╗████╗ ████║██║ +// ██╔██╗ ██║███████║██╔████╔██║██║ +// ██║╚██╗██║██╔══██║██║╚██╔╝██║██║ +// ██║ ╚████║██║ ██║██║ ╚═╝ ██║███████╗ +// ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ +// + +package codify + +import ( + "bytes" + "fmt" + "text/template" + "time" + + "github.com/kris-nova/logger" + networkingv1 "k8s.io/api/networking/v1" +) + +type IngressClass struct { + KubeObject *networkingv1.IngressClass + GoName string +} + +func NewIngressClass(obj *networkingv1.IngressClass) *IngressClass { + obj.ObjectMeta = cleanObjectMeta(obj.ObjectMeta) + return &IngressClass{ + KubeObject: obj, + GoName: goName(obj.Name), + } +} + +func (k IngressClass) Install() (string, []string) { + l, packages := Literal(k.KubeObject) + install := fmt.Sprintf(` + {{ .GoName }}IngressClass := %s + a.objects = append(a.objects, {{ .GoName }}IngressClass) + + if client != nil { + _, err = client.NetworkingV1().IngressClasss("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}IngressClass, v1.CreateOptions{}) + if err != nil { + return err + } + } +`, l) + + tpl := template.New(fmt.Sprintf("%s", time.Now().String())) + tpl.Parse(install) + buf := &bytes.Buffer{} + k.KubeObject.Name = sanitizeK8sObjectName(k.KubeObject.Name) + err := tpl.Execute(buf, k) + if err != nil { + logger.Debug(err.Error()) + } + return alias(buf.String(), "networkingv1"), packages +} + +func (k IngressClass) Uninstall() string { + uninstall := ` + if client != nil { + err = client.NetworkingV1().IngressClass("{{ .KubeObject.Namespace }}").Delete(context.TODO(), "{{ .KubeObject.Name }}", metav1.DeleteOptions{}) + if err != nil { + return err + } + } + ` + tpl := template.New(fmt.Sprintf("%s", time.Now().String())) + tpl.Parse(uninstall) + buf := &bytes.Buffer{} + err := tpl.Execute(buf, k) + if err != nil { + logger.Debug(err.Error()) + } + return buf.String() +} diff --git a/codify/validatingwebhookconfiguration.go b/codify/validatingwebhookconfiguration.go new file mode 100644 index 0000000..45ced1e --- /dev/null +++ b/codify/validatingwebhookconfiguration.go @@ -0,0 +1,92 @@ +// +// Copyright © 2021 Kris Nóva +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ███╗ ██╗ █████╗ ███╗ ███╗██╗ +// ████╗ ██║██╔══██╗████╗ ████║██║ +// ██╔██╗ ██║███████║██╔████╔██║██║ +// ██║╚██╗██║██╔══██║██║╚██╔╝██║██║ +// ██║ ╚████║██║ ██║██║ ╚═╝ ██║███████╗ +// ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ +// + +package codify + +import ( + "bytes" + "fmt" + "text/template" + "time" + + admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + + "github.com/kris-nova/logger" +) + +type ValidatingwebhookConfiguration struct { + KubeObject *admissionregistrationv1.ValidatingWebhookConfiguration + GoName string +} + +func NewValidatingwebhookConfiguration(obj *admissionregistrationv1.ValidatingWebhookConfiguration) *ValidatingwebhookConfiguration { + obj.ObjectMeta = cleanObjectMeta(obj.ObjectMeta) + return &ValidatingwebhookConfiguration{ + KubeObject: obj, + GoName: goName(obj.Name), + } +} + +func (k ValidatingwebhookConfiguration) Install() (string, []string) { + l, packages := Literal(k.KubeObject) + install := fmt.Sprintf(` + {{ .GoName }}ValidatingwebhookConfiguration := %s + a.objects = append(a.objects, {{ .GoName }}ValidatingwebhookConfiguration) + + if client != nil { + _, err = client.admissionregistrationv1().ValidatingwebhookConfigurations("{{ .KubeObject.Namespace }}").Create(context.TODO(), {{ .GoName }}ValidatingwebhookConfiguration, v1.CreateOptions{}) + if err != nil { + return err + } + } +`, l) + + tpl := template.New(fmt.Sprintf("%s", time.Now().String())) + tpl.Parse(install) + buf := &bytes.Buffer{} + k.KubeObject.Name = sanitizeK8sObjectName(k.KubeObject.Name) + err := tpl.Execute(buf, k) + if err != nil { + logger.Debug(err.Error()) + } + return alias(buf.String(), "admissionregistrationv1"), packages +} + +func (k ValidatingwebhookConfiguration) Uninstall() string { + uninstall := ` + if client != nil { + err = client.admissionregistrationv1().ValidatingwebhookConfigurations("{{ .KubeObject.Namespace }}").Delete(context.TODO(), "{{ .KubeObject.Name }}", metav1.DeleteOptions{}) + if err != nil { + return err + } + } + ` + tpl := template.New(fmt.Sprintf("%s", time.Now().String())) + tpl.Parse(uninstall) + buf := &bytes.Buffer{} + err := tpl.Execute(buf, k) + if err != nil { + logger.Debug(err.Error()) + } + return buf.String() +}