-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix main.go and add controller logic
- Loading branch information
Showing
18 changed files
with
1,297 additions
and
75 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
package sync | ||
|
||
var controllerLabels = map[string]string{ | ||
"app.kubernetes.io/managed-by": "tomcat-operator.apache.org", | ||
} |
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,66 @@ | ||
package sync | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/imdario/mergo" | ||
"github.com/kube-incubator/kube-operator-helper/mergo/transformers" | ||
"github.com/kube-incubator/kube-operator-helper/syncer" | ||
"github.com/kube-incubator/tomcat-operator/pkg/scheme/tomcat" | ||
) | ||
|
||
var ( | ||
oneReplica int32 = 1 | ||
) | ||
|
||
// NewDeploymentSyncer returns a new sync.Interface for reconciling tomcat Deployment | ||
func NewDeploymentSyncer(tc *tomcat.Tomcat, c client.Client, scheme *runtime.Scheme) syncer.Interface { | ||
objLabels := tc.ComponentLabels(tomcat.TomcatDeployment) | ||
|
||
obj := &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: tc.ComponentName(tomcat.TomcatDeployment), | ||
Namespace: tc.Namespace, | ||
}, | ||
} | ||
|
||
return syncer.NewObjectSyncer("Deployment", tc.Unwrap(), obj, c, scheme, func(existing runtime.Object) error { | ||
out := existing.(*appsv1.Deployment) | ||
out.Labels = labels.Merge(labels.Merge(out.Labels, objLabels), controllerLabels) | ||
|
||
template := tc.TomcatServerPodTemplateSpec() | ||
|
||
out.Spec.Template.ObjectMeta = template.ObjectMeta | ||
|
||
selector := metav1.SetAsLabelSelector(tc.TomcatServerPodLabels()) | ||
if !reflect.DeepEqual(selector, out.Spec.Selector) { | ||
if out.ObjectMeta.CreationTimestamp.IsZero() { | ||
out.Spec.Selector = selector | ||
} else { | ||
return fmt.Errorf("deployment selector is immutable") | ||
} | ||
} | ||
|
||
err := mergo.Merge(&out.Spec.Template.Spec, template.Spec, mergo.WithTransformers(transformers.PodSpec)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if tc.Spec.Replicas != nil { | ||
out.Spec.Replicas = tc.Spec.Replicas | ||
} | ||
|
||
if out.Spec.Replicas == nil { | ||
out.Spec.Replicas = &oneReplica | ||
} | ||
|
||
return nil | ||
}) | ||
} |
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,51 @@ | ||
package sync | ||
|
||
import ( | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/kube-incubator/kube-operator-helper/syncer" | ||
"github.com/kube-incubator/tomcat-operator/pkg/scheme/tomcat" | ||
) | ||
|
||
// NewServiceSyncer returns a new sync.Interface for reconciling Tomcat Service | ||
func NewServiceSyncer(tc *tomcat.Tomcat, c client.Client, scheme *runtime.Scheme) syncer.Interface { | ||
objLabels := tc.ComponentLabels(tomcat.TomcatDeployment) | ||
|
||
obj := &corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: tc.Name, | ||
Namespace: tc.Namespace, | ||
}, | ||
} | ||
|
||
return syncer.NewObjectSyncer("Service", tc.Unwrap(), obj, c, scheme, func(existing runtime.Object) error { | ||
out := existing.(*corev1.Service) | ||
out.Labels = labels.Merge(labels.Merge(out.Labels, objLabels), controllerLabels) | ||
|
||
selector := tc.TomcatServerPodLabels() | ||
if !labels.Equals(selector, out.Spec.Selector) { | ||
if out.ObjectMeta.CreationTimestamp.IsZero() { | ||
out.Spec.Selector = selector | ||
} else { | ||
return fmt.Errorf("service selector is immutable") | ||
} | ||
} | ||
|
||
if len(out.Spec.Ports) != 1 { | ||
out.Spec.Ports = make([]corev1.ServicePort, 1) | ||
} | ||
|
||
out.Spec.Ports[0].Name = "http" | ||
out.Spec.Ports[0].Port = *tc.Spec.ServicePort | ||
out.Spec.Ports[0].TargetPort = intstr.FromInt(tomcat.TomcatHTTPPort) | ||
|
||
return nil | ||
}) | ||
} |
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
Oops, something went wrong.