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

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

Timothee Gosselin's avatar
Timothee Gosselin committed
    https://www.gnu.org/licenses/agpl-3.0.html
Timothee Gosselin's avatar
Timothee Gosselin committed

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 (
Timothee Gosselin's avatar
Timothee Gosselin committed
	settings "k8s.libre.sh/application/settings"
Timothee Gosselin's avatar
Timothee Gosselin committed
	meta "k8s.libre.sh/meta"
Timothee Gosselin's avatar
Timothee Gosselin committed
	"k8s.libre.sh/objects"
Timothee Gosselin's avatar
Timothee Gosselin committed
	deployment "k8s.libre.sh/objects/deployment"

	corev1 "k8s.io/api/core/v1"
Timothee Gosselin's avatar
Timothee Gosselin committed
)

Timothee Gosselin's avatar
Timothee Gosselin committed
const DefaultComponent string = "app"

Timothee Gosselin's avatar
Timothee Gosselin committed
// +kubebuilder:object:generate=true
type Workload struct {
Timothee Gosselin's avatar
Timothee Gosselin committed
	*meta.ObjectMeta       `json:"commonMeta,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	*deployment.Deployment `json:"deployment,omitempty"`
Timothee Gosselin's avatar
Timothee Gosselin committed
	*Network               `json:"network,omitempty"`
	Dependencies           bool `json:"dependencies,omitempty"`
}

func (w *Workload) HasDependencies() bool {
	return w.Dependencies
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (w *Workload) GetMeta() meta.Instance {
	return w.ObjectMeta
Timothee Gosselin's avatar
Timothee Gosselin committed
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (w *Workload) GetObjects() map[int]objects.Object {
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	objects := make(map[int]objects.Object)
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	ing := &Ingress{
		// TODO TO FIX
		ObjectMeta: w.IngressMeta,
Timothee Gosselin's avatar
Timothee Gosselin committed
		Backends: []Backend{
			*w.Backend,
		},
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	srv := &Service{
		// TODO TO FIX
		ObjectMeta: w.ServiceMeta,
Timothee Gosselin's avatar
Timothee Gosselin committed
		Backend:    w.Backend,
	}

Timothee Gosselin's avatar
Timothee Gosselin committed
	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
Timothee Gosselin's avatar
Timothee Gosselin committed
	objects[2] = w.Deployment

	return objects
}

Timothee Gosselin's avatar
Timothee Gosselin committed
func (w *Workload) SetDependencies(s *settings.Component) { w.ConfigSpec = s.ConfigSpec }
func (w *Workload) GetDependencies() settings.Config      { return w.ConfigSpec }
Timothee Gosselin's avatar
Timothee Gosselin committed
func (w *Workload) SetDefaults() {
Timothee Gosselin's avatar
Timothee Gosselin committed

	// TODO FIXME
	w.Dependencies = true

Timothee Gosselin's avatar
Timothee Gosselin committed
	w.Backend.ServiceName = w.ServiceMeta.GetName()

	containerPort := corev1.ContainerPort{
		Name:          w.Backend.Port.Name,
		ContainerPort: w.Backend.Port.Port,
		Protocol:      w.Backend.Port.Protocol,
	}

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

Timothee Gosselin's avatar
Timothee Gosselin committed
}

func (w *Workload) Init() {

	// TODO improve initialisation of objectMeta, labels...
Timothee Gosselin's avatar
Timothee Gosselin committed
	if w.ObjectMeta == nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
		w.ObjectMeta = &meta.ObjectMeta{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

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

Timothee Gosselin's avatar
Timothee Gosselin committed
	// TODO FIXME
Timothee Gosselin's avatar
Timothee Gosselin committed
	if len(w.ObjectMeta.GetComponent()) == 0 {
		w.ObjectMeta.SetComponent(DefaultComponent)
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

	if w.Deployment.ObjectMeta == nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
		w.Deployment.ObjectMeta = &meta.ObjectMeta{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	if w.ConfigSpec == nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
		w.ConfigSpec = &settings.ConfigSpec{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
Timothee Gosselin's avatar
Timothee Gosselin committed

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

Timothee Gosselin's avatar
Timothee Gosselin committed
	if w.ServiceMeta == nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
		w.ServiceMeta = &meta.ObjectMeta{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}

	if w.IngressMeta == nil {
Timothee Gosselin's avatar
Timothee Gosselin committed
		w.IngressMeta = &meta.ObjectMeta{}
Timothee Gosselin's avatar
Timothee Gosselin committed
	}
Timothee Gosselin's avatar
Timothee Gosselin committed
}