Skip to content

Commit

Permalink
Allow for custom namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
earlgreyz committed Dec 12, 2018
1 parent c6663ad commit 87ef7df
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 22 deletions.
14 changes: 8 additions & 6 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/signal"
"syscall"

corev1 "k8s.io/api/core/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand All @@ -26,9 +27,10 @@ import (
)

var (
kubeconfig = flag.String("kubeconfig", "", "Path to kubeconfig. Only required if out-of-cluster.")
master = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
kubeconfig = flag.String("kubeconfig", "", "Path to kubeconfig. Only required if out-of-cluster")
master = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster")
debug = flag.Bool("debug", false, "Show debug logs")
namespace = flag.String("namespace", corev1.NamespaceDefault, "Create/Delete objects only on specific namespace")
)

var (
Expand Down Expand Up @@ -114,20 +116,20 @@ func initializeClientSets(config *rest.Config) error {
}

func initializeObjects() error {
err := clustercrd.CreateClusterCRD(extClientset)
err := clustercrd.CreateClusterCRD(*namespace, extClientset)
if err != nil {
return err
}

err = backupschedulecrd.CreateBackupScheduleCRD(extClientset)
err = backupschedulecrd.CreateBackupScheduleCRD(*namespace, extClientset)
if err != nil {
return err
}

err = backupinstancecrd.CreateBackupInstanceCRD(extClientset)
err = backupinstancecrd.CreateBackupInstanceCRD(*namespace, extClientset)
if err != nil {
return err
}

return operator.CreateConfigMap(kubeClientset)
return operator.CreateConfigMap(*namespace, kubeClientset)
}
4 changes: 2 additions & 2 deletions pkg/crd/backupinstance/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
)

// CreateBackupInstanceCRD registers a MySQLBackupInstance custom resource definition.
func CreateBackupInstanceCRD(clientset apiextensions.Interface) error {
err := crd.RegisterCRD(clientset, definitionFilename)
func CreateBackupInstanceCRD(namespace string, clientset apiextensions.Interface) error {
err := crd.RegisterCRD(namespace, clientset, definitionFilename)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/crd/backupinstance/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"time"

corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
Expand All @@ -31,7 +32,7 @@ var _ = Describe("CRD Backup Instance Create", func() {
It("should register Backup Instance CRD in the clientset", func(done Done) {
go func() {
defer GinkgoRecover()
err := CreateBackupInstanceCRD(clientset)
err := CreateBackupInstanceCRD(corev1.NamespaceAll, clientset)
Expect(err).NotTo(HaveOccurred())
crd, err := crdInterface.Get(CustomResourceName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -58,7 +59,7 @@ var _ = Describe("CRD Backup Instance Create", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should finish with no fail", func() {
err := CreateBackupInstanceCRD(clientset)
err := CreateBackupInstanceCRD(corev1.NamespaceAll, clientset)
Expect(err).NotTo(HaveOccurred())
crd, err := crdInterface.Get(CustomResourceName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expand Down
4 changes: 2 additions & 2 deletions pkg/crd/backupschedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
)

// CreateBackupScheduleCRD registers a MySQLBackupSchedule custom resource definition.
func CreateBackupScheduleCRD(clientset apiextensions.Interface) error {
err := crd.RegisterCRD(clientset, definitionFilename)
func CreateBackupScheduleCRD(namespace string, clientset apiextensions.Interface) error {
err := crd.RegisterCRD(namespace, clientset, definitionFilename)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/crd/backupschedule/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"time"

corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
Expand All @@ -31,7 +32,7 @@ var _ = Describe("CRD Backup Schedule Create", func() {
It("should register Backup Schedule CRD in the clientset", func(done Done) {
go func() {
defer GinkgoRecover()
err := CreateBackupScheduleCRD(clientset)
err := CreateBackupScheduleCRD(corev1.NamespaceAll, clientset)
Expect(err).NotTo(HaveOccurred())
crd, err := crdInterface.Get(CustomResourceName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -58,7 +59,7 @@ var _ = Describe("CRD Backup Schedule Create", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should finish with no fail", func() {
err := CreateBackupScheduleCRD(clientset)
err := CreateBackupScheduleCRD(corev1.NamespaceAll, clientset)
Expect(err).NotTo(HaveOccurred())
crd, err := crdInterface.Get(CustomResourceName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expand Down
4 changes: 2 additions & 2 deletions pkg/crd/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
)

// CreateClusterCRD registers a MySQLCluster custom resource in kubernetes api.
func CreateClusterCRD(clientset apiextensions.Interface) error {
err := crd.RegisterCRD(clientset, definitionFilename)
func CreateClusterCRD(namespace string, clientset apiextensions.Interface) error {
err := crd.RegisterCRD(namespace, clientset, definitionFilename)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/crd/cluster/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"time"

corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
Expand All @@ -31,7 +32,7 @@ var _ = Describe("CRD Cluster Create", func() {
It("should register cluster CRD in the clientset", func(done Done) {
go func() {
defer GinkgoRecover()
err := CreateClusterCRD(clientset)
err := CreateClusterCRD(corev1.NamespaceAll, clientset)
Expect(err).NotTo(HaveOccurred())
crd, err := crdInterface.Get(CustomResourceName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -58,7 +59,7 @@ var _ = Describe("CRD Cluster Create", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should finish with no fail", func() {
err := CreateClusterCRD(clientset)
err := CreateClusterCRD(corev1.NamespaceAll, clientset)
Expect(err).NotTo(HaveOccurred())
crd, err := crdInterface.Get(CustomResourceName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expand Down
5 changes: 3 additions & 2 deletions pkg/crd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
)

// RegisterCRD registers given custom resource definition into the kubernetes api.
func RegisterCRD(clientset apiextensions.Interface, filename string) error {
func RegisterCRD(namespace string, clientset apiextensions.Interface, filename string) error {
crd := new(apiextensionsv1.CustomResourceDefinition)
err := util.ObjectFromFile(filename, crd)
if err != nil {
return err
}

crdInterface := clientset.ApiextensionsV1beta1().CustomResourceDefinitions()
crd.SetNamespace(namespace)

crdInterface := clientset.ApiextensionsV1beta1().CustomResourceDefinitions()
_, err = crdInterface.Create(crd)
if err != nil && apierrors.IsAlreadyExists(err) {
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/cluster/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
)

// CreateConfigMap registers a "mysql" config map used by MySQL Clusters.
func CreateConfigMap(clientset kubernetes.Interface) error {
func CreateConfigMap(namespace string, clientset kubernetes.Interface) error {
configMap := new(corev1.ConfigMap)
err := util.ObjectFromFile("artifacts/cluster-configmap.yaml", configMap)
if err != nil {
return err
}

_, err = clientset.CoreV1().ConfigMaps(corev1.NamespaceDefault).Create(configMap)
_, err = clientset.CoreV1().ConfigMaps(namespace).Create(configMap)
if err != nil && apierrors.IsAlreadyExists(err) {
return nil
}
Expand Down

0 comments on commit 87ef7df

Please sign in to comment.