-
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.
add useful scheme wrapper for v1alpha1.Tomcat
- Loading branch information
Showing
4 changed files
with
154 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package tomcat | ||
|
||
var ( | ||
defaultServicePort int32 = 80 | ||
) | ||
|
||
const ( | ||
defaultImage string = "tomcat:latest" | ||
) | ||
|
||
// SetDefaults sets Tomcat field defaults | ||
func (o *Tomcat) SetDefaults() { | ||
|
||
if len(o.Spec.Image) == 0 { | ||
o.Spec.Image = defaultImage | ||
} | ||
|
||
if o.Spec.ServicePort == nil { | ||
o.Spec.ServicePort = &defaultServicePort | ||
} | ||
} |
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,30 @@ | ||
package tomcat | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
tomcatHTTPPort = 8080 | ||
) | ||
|
||
// TomcatServerPodTemplateSpec generates a pod template spec suitable for use in Tomcat deployment | ||
func (tomcat *Tomcat) TomcatServerPodTemplateSpec() (out corev1.PodTemplateSpec) { | ||
out = corev1.PodTemplateSpec{} | ||
out.ObjectMeta.Labels = tomcat.TomcatServerPodLabels() | ||
|
||
out.Spec.Containers = []corev1.Container{ | ||
{ | ||
Name: "tomcat", | ||
Image: tomcat.Spec.Image, | ||
Ports: []corev1.ContainerPort{ | ||
{ | ||
Name: "http", | ||
ContainerPort: int32(tomcatHTTPPort), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return out | ||
} |
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,77 @@ | ||
package tomcat | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/labels" | ||
|
||
tomcatv1alpha1 "github.com/kube-incubator/tomcat-operator/pkg/apis/tomcat/v1alpha1" | ||
) | ||
|
||
// Tomcat embeds tomcatv1alpha1.Tomcat and adds utility functions | ||
type Tomcat struct { | ||
*tomcatv1alpha1.Tomcat | ||
} | ||
|
||
type component struct { | ||
name string | ||
objNameFmt string | ||
objName string | ||
} | ||
|
||
var ( | ||
// TomcatDeployment component | ||
TomcatDeployment = component{name: "tomcat-server", objNameFmt: "%s"} | ||
// TomcatService component | ||
TomcatService = component{name: "tomcat-server", objNameFmt: "%s"} | ||
) | ||
|
||
// New wraps a tomcatv1alpha1.Tomcat into a Tomcat object | ||
func New(obj *tomcatv1alpha1.Tomcat) *Tomcat { | ||
return &Tomcat{obj} | ||
} | ||
|
||
// Unwrap returns the wrapped tomcatv1alpha1.Tomcat object | ||
func (o *Tomcat) Unwrap() *tomcatv1alpha1.Tomcat { | ||
return o.Tomcat | ||
} | ||
|
||
// Labels returns default label set for tomcatv1alpha1.Tomcat | ||
func (o *Tomcat) Labels() labels.Set { | ||
partOf := "tomcat" | ||
if o.ObjectMeta.Labels != nil && len(o.ObjectMeta.Labels["app.kubernetes.io/part-of"]) > 0 { | ||
partOf = o.ObjectMeta.Labels["app.kubernetes.io/part-of"] | ||
} | ||
|
||
labels := labels.Set{ | ||
"app.kubernetes.io/name": "tomcat", | ||
"app.kubernetes.io/part-of": partOf, | ||
"app.kubernetes.io/instance": o.ObjectMeta.Name, | ||
} | ||
|
||
return labels | ||
} | ||
|
||
// ComponentLabels returns labels for a label set for a tomcatv1alpha1.Tomcat component | ||
func (o *Tomcat) ComponentLabels(component component) labels.Set { | ||
l := o.Labels() | ||
l["app.kubernetes.io/component"] = component.name | ||
return l | ||
} | ||
|
||
// ComponentName returns the object name for a component | ||
func (o *Tomcat) ComponentName(component component) string { | ||
name := component.objName | ||
if len(component.objNameFmt) > 0 { | ||
name = fmt.Sprintf(component.objNameFmt, o.ObjectMeta.Name) | ||
} | ||
|
||
return name | ||
} | ||
|
||
// TomcatServerPodLabels return labels to apply to tomcat server pods | ||
func (o *Tomcat) TomcatServerPodLabels() labels.Set { | ||
l := o.Labels() | ||
l["app.kubernetes.io/component"] = "tomcat-server" | ||
return l | ||
} |