Skip to content

Commit

Permalink
refactor: move resources to their respective packages, split names to…
Browse files Browse the repository at this point in the history
… avoid cycle
  • Loading branch information
metacosm committed Oct 22, 2019
1 parent a357f72 commit 27e570c
Show file tree
Hide file tree
Showing 31 changed files with 81 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package capability

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package capability

import (
"halkyon.io/operator/pkg/controller/framework"
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/capability/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package capability

import (
"halkyon.io/api/capability/v1beta1"
controller2 "halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
"k8s.io/apimachinery/pkg/runtime"
)
Expand Down Expand Up @@ -52,11 +51,11 @@ func (r *CapabilityManager) PrimaryResourceType() runtime.Object {
}

func (r *CapabilityManager) NewFrom(name string, namespace string) (framework.Resource, error) {
return controller2.NewCapability().FetchAndInit(name, namespace, r)
return NewCapability().FetchAndInit(name, namespace, r)
}

func asCapability(object runtime.Object) *controller2.Capability {
return object.(*controller2.Capability)
func asCapability(object runtime.Object) *Capability {
return object.(*Capability)
}

func (r *CapabilityManager) Delete(object framework.Resource) error {
Expand Down
6 changes: 1 addition & 5 deletions pkg/controller/capability/database.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package capability

import (
"halkyon.io/operator/pkg/controller"
)

func (r *CapabilityManager) installDB(c *controller.Capability) (e error) {
func (r *CapabilityManager) installDB(c *Capability) (e error) {
return c.CreateOrUpdate(r.Helper())
}
7 changes: 3 additions & 4 deletions pkg/controller/capability/kubedb_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/appscode/go/encoding/json/types"
kubedbv1 "github.com/kubedb/apimachinery/apis/kubedb/v1alpha1"
"halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
Expand Down Expand Up @@ -36,12 +35,12 @@ func newOwnedPostgres(owner framework.Resource) postgres {
return p
}

func (res postgres) ownerAsCapability() *controller.Capability {
return res.Owner().(*controller.Capability)
func (res postgres) ownerAsCapability() *Capability {
return res.Owner().(*Capability)
}

func (res postgres) Name() string {
return controller.PostgresName(res.Owner())
return PostgresName(res.Owner())
}

//buildSecret returns the postgres resource
Expand Down
13 changes: 13 additions & 0 deletions pkg/controller/capability/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package capability

import (
"halkyon.io/operator/pkg/controller/framework"
)

func PostgresName(owner framework.Resource) string {
return framework.DefaultDependentResourceNameFor(owner)
}

func ServiceAccountName(owner framework.Resource) string {
return PostgresName(owner) // todo: fix me
}
7 changes: 5 additions & 2 deletions pkg/controller/capability/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ type roleBinding struct {
}

func newRoleBinding(owner framework.Resource) roleBinding {
rb := controller.NewOwnedRoleBinding(owner, func() string { return "use-scc-privileged" }, func() string { return newRole(owner).Name() })
rb := controller.NewOwnedRoleBinding(owner,
func() string { return "use-scc-privileged" },
func() string { return newRole(owner).Name() },
func() string { return newOwnedPostgres(owner).Name() })
return roleBinding{RoleBinding: rb}
}

Expand All @@ -28,7 +31,7 @@ func (res roleBinding) Build() (runtime.Object, error) {

ser := build.(*authorizv1.RoleBinding)
owner := res.Owner()
ser.Subjects = append(ser.Subjects, authorizv1.Subject{Kind: "ServiceAccount", Name: controller.PostgresName(owner), Namespace: owner.GetNamespace()})
ser.Subjects = append(ser.Subjects, authorizv1.Subject{Kind: "ServiceAccount", Name: PostgresName(owner), Namespace: owner.GetNamespace()})

return ser, nil
}
5 changes: 2 additions & 3 deletions pkg/controller/capability/secret.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package capability

import (
"halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -31,8 +30,8 @@ func newOwnedSecret(owner framework.Resource) secret {
return s
}

func (res secret) ownerAsCapability() *controller.Capability {
return res.Owner().(*controller.Capability)
func (res secret) ownerAsCapability() *Capability {
return res.Owner().(*Capability)
}

//buildSecret returns the secret resource
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/component/build_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package component

import (
component "halkyon.io/api/component/v1beta1"
"halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
"k8s.io/api/apps/v1"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -15,7 +14,7 @@ import (
//createBuildDeployment returns the Deployment config object to be used for deployment using a container image build by Tekton
func (res deployment) installBuild() (runtime.Object, error) {
c := res.ownerAsComponent()
ls := getAppLabels(controller.DeploymentName(c))
ls := getAppLabels(DeploymentName(c))

// create runtime container using built image (= created by the Tekton build task)
runtimeContainer, err := getRuntimeContainerFor(c)
Expand All @@ -27,7 +26,7 @@ func (res deployment) installBuild() (runtime.Object, error) {
// and we will enrich the deployment resource of the runtime container
// create a "dev" version of the component to be able to check if the dev deployment exists
devDeployment := &appsv1.Deployment{}
_, err = framework.GetHelperFor(c.GetAPIObject()).Fetch(controller.DeploymentNameFor(c, component.DevDeploymentMode), c.Namespace, devDeployment)
_, err = framework.GetHelperFor(c.GetAPIObject()).Fetch(DeploymentNameFor(c, component.DevDeploymentMode), c.Namespace, devDeployment)
if err == nil {
devContainer := &devDeployment.Spec.Template.Spec.Containers[0]
runtimeContainer.Env = devContainer.Env
Expand Down Expand Up @@ -70,7 +69,7 @@ func (res deployment) installBuild() (runtime.Object, error) {
return dep, nil
}

func getRuntimeContainerFor(component *controller.Component) (corev1.Container, error) {
func getRuntimeContainerFor(component *Component) (corev1.Container, error) {
container := corev1.Container{
Env: populatePodEnvVar(component.Spec),
Image: dockerImageURL(component),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package component

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package component

import (
"halkyon.io/operator/pkg/controller/framework"
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/component/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package component
import (
"context"
component "halkyon.io/api/component/v1beta1"
controller2 "halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -63,11 +62,11 @@ func (r *ComponentManager) PrimaryResourceType() runtime.Object {
}

func (r *ComponentManager) NewFrom(name string, namespace string) (framework.Resource, error) {
return controller2.NewComponent().FetchAndInit(name, namespace, r)
return NewComponent().FetchAndInit(name, namespace, r)
}

func (ComponentManager) asComponent(object runtime.Object) *controller2.Component {
return object.(*controller2.Component)
func (ComponentManager) asComponent(object runtime.Object) *Component {
return object.(*Component)
}

func (r *ComponentManager) CreateOrUpdate(object framework.Resource) (err error) {
Expand Down
9 changes: 4 additions & 5 deletions pkg/controller/component/dependent.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package component

import (
"halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
"k8s.io/apimachinery/pkg/runtime"
)
Expand All @@ -18,10 +17,10 @@ func newBaseDependent(primaryResourceType runtime.Object, owner framework.Resour
return base{DependentResourceHelper: framework.NewDependentResource(primaryResourceType, owner)}
}

func (res base) ownerAsComponent() *controller.Component {
return res.Owner().(*controller.Component)
func (res base) ownerAsComponent() *Component {
return res.Owner().(*Component)
}

func (res base) asComponent(object runtime.Object) *controller.Component {
return object.(*controller.Component)
func (res base) asComponent(object runtime.Object) *Component {
return object.(*Component)
}
3 changes: 1 addition & 2 deletions pkg/controller/component/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package component

import (
component "halkyon.io/api/component/v1beta1"
"halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -36,5 +35,5 @@ func (res deployment) Build() (runtime.Object, error) {
}

func (res deployment) Name() string {
return controller.DeploymentName(res.ownerAsComponent())
return DeploymentName(res.ownerAsComponent())
}
3 changes: 1 addition & 2 deletions pkg/controller/component/dev_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package component

import (
component "halkyon.io/api/component/v1beta1"
"halkyon.io/operator/pkg/controller"
"k8s.io/api/apps/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -13,7 +12,7 @@ import (
//buildDevDeployment returns the Deployment config object
func (res deployment) installDev() (runtime.Object, error) {
c := res.ownerAsComponent()
ls := getAppLabels(controller.DeploymentName(c))
ls := getAppLabels(DeploymentName(c))

// create runtime container
runtimeContainer, err := getBaseContainerFor(c.Component)
Expand Down
15 changes: 7 additions & 8 deletions pkg/controller/component/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package component
import (
component "halkyon.io/api/component/v1beta1"
"halkyon.io/api/v1beta1"
"halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
"os"
)
Expand Down Expand Up @@ -36,7 +35,7 @@ func getEnvAsMap(component component.ComponentSpec) (map[string]string, error) {
return tmpEnvVar, nil
}

func populateEnvVar(component *controller.Component) {
func populateEnvVar(component *Component) {
tmpEnvVar, err := getEnvAsMap(component.Spec)
if err != nil {
panic(err)
Expand Down Expand Up @@ -69,7 +68,7 @@ func getBuildLabels(name string) map[string]string {
}
}

func (r *ComponentManager) PopulateK8sLabels(component *controller.Component, componentType string) map[string]string {
func (r *ComponentManager) PopulateK8sLabels(component *Component, componentType string) map[string]string {
labels := map[string]string{}
labels[v1beta1.RuntimeLabelKey] = component.Spec.Runtime
labels[v1beta1.RuntimeVersionLabelKey] = component.Spec.Version
Expand All @@ -79,7 +78,7 @@ func (r *ComponentManager) PopulateK8sLabels(component *controller.Component, co
return labels
}

func baseImage(c *controller.Component) string {
func baseImage(c *Component) string {
if c.Spec.BuildConfig.BaseImage != "" {
return c.Spec.BuildConfig.BaseImage
} else {
Expand All @@ -93,7 +92,7 @@ func baseImage(c *controller.Component) string {
}
}

func contextPath(c *controller.Component) string {
func contextPath(c *Component) string {
if c.Spec.BuildConfig.ContextPath != "" {
return c.Spec.BuildConfig.ContextPath
} else {
Expand All @@ -102,7 +101,7 @@ func contextPath(c *controller.Component) string {
}
}

func moduleDirName(c *controller.Component) string {
func moduleDirName(c *Component) string {
if c.Spec.BuildConfig.ModuleDirName != "" {
return c.Spec.BuildConfig.ModuleDirName
} else {
Expand All @@ -111,15 +110,15 @@ func moduleDirName(c *controller.Component) string {
}
}

func gitRevision(c *controller.Component) string {
func gitRevision(c *Component) string {
if c.Spec.BuildConfig.Ref == "" {
return "master"
} else {
return c.Spec.BuildConfig.Ref
}
}

func dockerImageURL(c *controller.Component) string {
func dockerImageURL(c *Component) string {
// Try to find the registry env var
registry, found := os.LookupEnv(RegistryAddressEnvVar)
if found {
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/component/ingress.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package component

import (
"halkyon.io/operator/pkg/controller"
"halkyon.io/operator/pkg/controller/framework"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -30,7 +29,7 @@ func newOwnedIngress(owner framework.Resource) ingress {

func (res ingress) Build() (runtime.Object, error) {
c := res.ownerAsComponent()
ls := getAppLabels(controller.DeploymentName(c))
ls := getAppLabels(DeploymentName(c))
ingress := &v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: res.Name(),
Expand Down
6 changes: 1 addition & 5 deletions pkg/controller/component/mode-build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ limitations under the License.

package component

import (
halkyon "halkyon.io/operator/pkg/controller"
)

func (r *ComponentManager) installBuildMode(component *halkyon.Component, namespace string) (e error) {
func (r *ComponentManager) installBuildMode(component *Component, namespace string) (e error) {
return component.CreateOrUpdate(r.Helper())
}
3 changes: 1 addition & 2 deletions pkg/controller/component/mode-dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ package component

import (
"fmt"
halkyon "halkyon.io/operator/pkg/controller"
)

func (r *ComponentManager) installDevMode(component *halkyon.Component, namespace string) (e error) {
func (r *ComponentManager) installDevMode(component *Component, namespace string) (e error) {
component.ObjectMeta.Namespace = namespace
// Enrich Component with k8s recommend Labels
component.ObjectMeta.Labels = r.PopulateK8sLabels(component, "Backend")
Expand Down
17 changes: 2 additions & 15 deletions pkg/controller/names.go → pkg/controller/component/names.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package controller
package component

import (
"fmt"
halkyon "halkyon.io/api/component/v1beta1"
"halkyon.io/operator/pkg/controller/framework"
"halkyon.io/operator/pkg/util"
)

func PostgresName(owner framework.Resource) string {
return framework.DefaultDependentResourceNameFor(owner)
}

func DeploymentName(c *Component) string {
return DeploymentNameFor(c, c.Spec.DeploymentMode)
}
Expand All @@ -32,14 +26,7 @@ func PVCName(c *Component) string {
}

func ServiceAccountName(owner framework.Resource) string {
switch owner.(type) {
case *Capability:
return PostgresName(owner) // todo: fix me
case *Component:
return "build-bot"
default:
panic(fmt.Sprintf("a service account shouldn't be created for '%s' %s owner", owner.GetName(), util.GetObjectName(owner)))
}
return "build-bot"
}

func TaskName(owner framework.Resource) string {
Expand Down
Loading

0 comments on commit 27e570c

Please sign in to comment.