Skip to content
workload.go 2.56 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
/*

Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.gnu.org/licenses/agpl-3.0.html

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 components

import (
	settings "k8s.libre.sh/application/settings"
	meta "k8s.libre.sh/meta"
	"k8s.libre.sh/objects"
	deployment "k8s.libre.sh/objects/deployment"

	corev1 "k8s.io/api/core/v1"
)

const DefaultComponent string = "app"

// +kubebuilder:object:generate=true
type Workload struct {
	*meta.ObjectMeta       `json:"commonMeta,omitempty"`
	*deployment.Deployment `json:"deployment,omitempty"`
	*Network               `json:"network,omitempty"`
	Settings               []string `json:"settings,omitempty"`
}

func (w *Workload) HasSettings() []string {
	return w.Settings
Timothee Gosselin's avatar
Timothee Gosselin committed
}

func (w *Workload) GetMeta() meta.Instance {
	return w.ObjectMeta
}

func (w *Workload) GetObjects() map[int]objects.Object {

	objects := make(map[int]objects.Object)

	srv := w.Network.GetService()
	ing := w.Network.GetIngress()
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	// TODO TO FIX
	//meta.SetObjectMeta(w.ObjectMeta, ing.ObjectMeta)
	//meta.SetObjectMeta(w.ObjectMeta, srv.ObjectMeta)
Timothee Gosselin's avatar
Timothee Gosselin committed

	objects[0] = srv
	objects[1] = ing
	objects[2] = w.Deployment

	return objects
}

func (w *Workload) GetConfig() settings.Config { return w.SettingsSpec }
Timothee Gosselin's avatar
Timothee Gosselin committed

func (w *Workload) SetDefaults() {

	for _, o := range w.GetObjects() {
		meta.SetObjectMeta(w.ObjectMeta, o)
	}

	if len(w.ServiceMeta.GetName()) > 0 {
		w.HTTPBackend.ServiceName = w.ServiceMeta.GetName()
Timothee Gosselin's avatar
Timothee Gosselin committed

	containerPort := corev1.ContainerPort{
		Name:          w.HTTPBackend.Port.Name,
		ContainerPort: w.HTTPBackend.Port.Port,
		Protocol:      w.HTTPBackend.Port.Protocol,
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	w.ContainerSpec.Ports = append(w.ContainerSpec.Ports, containerPort)

}

func (w *Workload) Init() {

	// TODO improve initialisation of objectMeta, labels...
	if w.ObjectMeta == nil {
		w.ObjectMeta = &meta.ObjectMeta{}
	}

	if len(w.ObjectMeta.Labels) == 0 {
		w.ObjectMeta.Labels = make(map[string]string)
	}

	// TODO FIXME
	if len(w.ObjectMeta.GetComponent()) == 0 {
		w.ObjectMeta.SetComponent(DefaultComponent)
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	if w.Deployment == nil {
		w.Deployment = &deployment.Deployment{}
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	w.Deployment.Init()
Timothee Gosselin's avatar
Timothee Gosselin committed

	if w.Network == nil {
		w.Network = &Network{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	w.Network.Init()
Timothee Gosselin's avatar
Timothee Gosselin committed
}