-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAJOR: crd: add job for custom resource definition handling
CRDs are not properly handled with external tools, Helm and similar options cannot handle upgrading of custom resource definitions
- Loading branch information
Showing
18 changed files
with
348 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ kubernetes-ingress | |
dist/ | ||
.code-generator/ | ||
bin/golangci-lint | ||
.local/* |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package definition | ||
|
||
import _ "embed" | ||
|
||
//go:embed defaults.core.haproxy.org.yaml | ||
var DefaultsV1alpha2 []byte | ||
|
||
//go:embed globals.core.haproxy.org.yaml | ||
var GlobalsV1alpha2 []byte | ||
|
||
//go:embed backends.core.haproxy.org.yaml | ||
var BackendsV1alpha2 []byte | ||
|
||
//go:embed upgrade/defaults.core.haproxy.org.yaml | ||
var DefaultsV1alpha1V1alpha2 []byte | ||
|
||
//go:embed upgrade/globals.core.haproxy.org.yaml | ||
var GlobalsV1alpha1V1alpha2 []byte | ||
|
||
//go:embed upgrade/backends.core.haproxy.org.yaml | ||
var BackendsV1alpha1V1alpha2 []byte | ||
|
||
func GetCRDs() map[string][]byte { | ||
return map[string][]byte{ | ||
"defaults.core.haproxy.org": DefaultsV1alpha2, | ||
"globals.core.haproxy.org": GlobalsV1alpha2, | ||
"backends.core.haproxy.org": BackendsV1alpha2, | ||
} | ||
} | ||
|
||
func GetCRDsUpgrade() map[string][]byte { | ||
return map[string][]byte{ | ||
"defaults.core.haproxy.org": DefaultsV1alpha1V1alpha2, | ||
"globals.core.haproxy.org": GlobalsV1alpha1V1alpha2, | ||
"backends.core.haproxy.org": BackendsV1alpha1V1alpha2, | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: haproxy-ingress-crd # each deploymnent should have a unique name, in example we always recreate the custer | ||
namespace: haproxy-controller | ||
spec: | ||
template: | ||
spec: | ||
serviceAccountName: haproxy-kubernetes-ingress-crd | ||
containers: | ||
- name: haproxy-ingress-crd | ||
image: haproxytech/kubernetes-ingress:latest | ||
imagePullPolicy: Never | ||
command: ["./haproxy-ingress-controller","--job-check-crd"] | ||
restartPolicy: Never | ||
backoffLimit: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: haproxy-kubernetes-ingress-crd | ||
namespace: haproxy-controller | ||
--- | ||
kind: ClusterRole | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: haproxy-kubernetes-ingress-crd | ||
rules: | ||
- apiGroups: | ||
- "apiextensions.k8s.io" | ||
resources: | ||
- customresourcedefinitions | ||
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] | ||
--- | ||
kind: ClusterRoleBinding | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: haproxy-kubernetes-ingress-crd | ||
namespace: haproxy-controller | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: haproxy-kubernetes-ingress-crd | ||
subjects: | ||
- kind: ServiceAccount | ||
name: haproxy-kubernetes-ingress-crd | ||
namespace: haproxy-controller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2023 HAProxy Technologies LLC | ||
// | ||
// 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 job | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/haproxytech/kubernetes-ingress/crs/definition" | ||
"github.com/haproxytech/kubernetes-ingress/pkg/k8s" | ||
"github.com/haproxytech/kubernetes-ingress/pkg/utils" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" | ||
apiError "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func CRDRefresh(log utils.Logger, osArgs utils.OSArgs) error { | ||
log.Info("checking CRDS") | ||
config, err := k8s.GetRestConfig(osArgs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create a new clientset for the apiextensions API group | ||
clientset := apiextensionsclientset.NewForConfigOrDie(config) | ||
|
||
// Check if the CRD exists | ||
crds := definition.GetCRDs() | ||
crdsUpgrade := definition.GetCRDsUpgrade() | ||
for crdName, crdDef := range crds { | ||
// CustomResourceDefinition object | ||
var crd apiextensionsv1.CustomResourceDefinition | ||
err = yaml.Unmarshal(crdDef, &crd) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info("") | ||
log.Infof("checking CRD %s", crdName) | ||
|
||
existingVersion, err := clientset.ApiextensionsV1().CustomResourceDefinitions().Get(context.Background(), crdName, metav1.GetOptions{}) | ||
if err != nil { | ||
if !apiError.IsNotFound(err) { | ||
return err | ||
} | ||
log.Infof("CRD %s does not exist", crdName) | ||
// Create the CRD | ||
_, err = clientset.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), &crd, metav1.CreateOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
log.Infof("CRD %s created", crdName) | ||
continue | ||
} | ||
log.Infof("CRD %s exists", crdName) | ||
versions := existingVersion.Spec.Versions | ||
if len(versions) == 2 { | ||
log.Infof("CRD %s exists as v1alpha1 and v1alpha2, nothing to do", crdName) | ||
continue | ||
} | ||
// check if we have alpha 2 or we need to upgrade for alpha2 | ||
crd.ObjectMeta.ResourceVersion = existingVersion.ObjectMeta.ResourceVersion | ||
if versions[0].Name == "v1alpha2" { | ||
log.Infof("CRD %s exists as v1alpha2, nothing to do", crdName) | ||
continue | ||
} | ||
err = yaml.Unmarshal(crdsUpgrade[crdName], &crd) | ||
if err != nil { | ||
return err | ||
} | ||
// Upgrade the CRDl | ||
_, err = clientset.ApiextensionsV1().CustomResourceDefinitions().Update(context.Background(), &crd, metav1.UpdateOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
log.Info("") | ||
log.Info("CRD update done") | ||
return nil | ||
} | ||
|
||
// IngressControllerCRDUpdater console pretty print | ||
const IngressControllerCRDUpdater = ` | ||
____ ____ ____ _ _ _ _ | ||
/ ___| _ \| _ \ | | | |_ __ __| | __ _| |_ ___ _ __ | ||
| | | |_) | | | | | | | | '_ \ / _` + "`" + ` |/ _` + "`" + ` | __/ _ \ '__| | ||
| |___| _ <| |_| | | |_| | |_) | (_| | (_| | || __/ | | ||
\____|_| \_\____/ \___/| .__/ \__,_|\__,_|\__\___|_| | ||
|_| | ||
` |
Oops, something went wrong.