Newer
Older
/*
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"
service "k8s.libre.sh/objects/service"
corev1 "k8s.io/api/core/v1"
)
// const DefaultComponent string = "app"
// +kubebuilder:object:generate=true
// InternalWorkload implements the component interface.
// It is a component that contains a deployment, service and some settings.
type InternalWorkload struct {
*meta.ObjectMeta `json:"commonMeta,omitempty"`
*deployment.Deployment `json:"deployment,omitempty"`
*service.Service `json:"service,omitempty"`
Settings []string `json:"settings,omitempty"`
}
// HasSettings returns a list of settings name that the component depends on
func (w *InternalWorkload) HasSettings() []string {
return w.Settings
}
func (w *InternalWorkload) GetMeta() meta.Instance {
return w.ObjectMeta
}
// GetObjects returns an ordered list of objects.Object interfaces
func (w *InternalWorkload) GetObjects() map[int]objects.Object {
objects := make(map[int]objects.Object)
objects[1] = w.Deployment
return objects
}
func (w *InternalWorkload) GetConfig() settings.Config { return w.SettingsSpec }
// SetDefaults sets the default values of the component.
// Container ports are set from service if not specified
func (w *InternalWorkload) SetDefaults() {
for _, o := range w.GetObjects() {
meta.SetObjectMeta(w.ObjectMeta, o)
}
containerPort := corev1.ContainerPort{
Name: w.Service.Port.Name,
ContainerPort: w.Service.Port.Port,
Protocol: w.Service.Port.Protocol,
}
w.ContainerSpec.Ports = append(w.ContainerSpec.Ports, containerPort)
}
func (w *InternalWorkload) 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)
}
if w.Deployment == nil {
w.Deployment = &deployment.Deployment{}
}
if w.Service == nil {
w.Service = &service.Service{}