Skip to content

Commit

Permalink
Merge pull request #85 from kris-nova/ingress-class
Browse files Browse the repository at this point in the history
adding ingressclass and validatingwebhookconfiguration
  • Loading branch information
krisnova authored Nov 1, 2021
2 parents 870f21b + 3f628ef commit 7c23fcf
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down
6 changes: 6 additions & 0 deletions codify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"strings"
"text/template"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"

"k8s.io/apimachinery/pkg/runtime"

"github.com/fatih/color"
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion codify/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
91 changes: 91 additions & 0 deletions codify/ingressclass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Copyright © 2021 Kris Nóva <[email protected]>
//
// 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()
}
92 changes: 92 additions & 0 deletions codify/validatingwebhookconfiguration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// Copyright © 2021 Kris Nóva <[email protected]>
//
// 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()
}

0 comments on commit 7c23fcf

Please sign in to comment.