Skip to content

Commit

Permalink
feat(traits): master refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Mar 22, 2024
1 parent 2f595d1 commit e06a8cf
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 65 deletions.
167 changes: 103 additions & 64 deletions addons/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/client"
"github.com/apache/camel-k/v2/pkg/metadata"
"github.com/apache/camel-k/v2/pkg/resources"
"github.com/apache/camel-k/v2/pkg/trait"
Expand Down Expand Up @@ -85,10 +86,13 @@ var (
)

func (t *masterTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition, error) {
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, true) {
if e.Integration == nil {
return false, nil, nil
}
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() {
if !pointer.BoolDeref(t.Enabled, true) {
return false, trait.NewIntegrationConditionUserDisabled(), nil
}
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization, v1.IntegrationPhaseBuildingKit) && !e.IntegrationInRunningPhases() {
return false, nil, nil
}
if pointer.BoolDeref(t.Auto, true) {
Expand All @@ -110,10 +114,12 @@ func (t *masterTrait) Configure(e *trait.Environment) (bool, *trait.TraitConditi
t.Enabled = &enabled
}
}
// No master component, can skip the trait execution
if !pointer.BoolDeref(t.Enabled, false) {
return false, nil, nil
}
}
if !pointer.BoolDeref(t.Enabled, false) {
return false, trait.NewIntegrationConditionUserDisabled(), nil
}

if t.IncludeDelegateDependencies == nil || *t.IncludeDelegateDependencies {
t.delegateDependencies = findAdditionalDependencies(e, meta)
}
Expand Down Expand Up @@ -145,83 +151,81 @@ func (t *masterTrait) Configure(e *trait.Environment) (bool, *trait.TraitConditi
}
}

return pointer.BoolDeref(t.Enabled, true), nil, nil
return pointer.BoolDeref(t.Enabled, false), nil, nil
}

func (t *masterTrait) Apply(e *trait.Environment) error {
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityMaster)

// Master sub endpoints need to be added to the list of dependencies
for _, dep := range t.delegateDependencies {
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, dep)
}

} else if e.IntegrationInRunningPhases() {
serviceAccount := e.Integration.Spec.ServiceAccountName
if serviceAccount == "" {
serviceAccount = "default"
}

templateData := struct {
Namespace string
Name string
ServiceAccount string
}{
Namespace: e.Integration.Namespace,
Name: fmt.Sprintf("%s-master", e.Integration.Name),
ServiceAccount: serviceAccount,
}

roleSuffix := leaseResourceType
if t.ResourceType != nil {
roleSuffix = *t.ResourceType
}
roleSuffix = strings.ToLower(roleSuffix)

role, err := loadResource(e, fmt.Sprintf("master-role-%s.tmpl", roleSuffix), templateData)
// Master trait requires the ServiceAccount certain privileges
privileges, err := t.prepareRBAC(e.Client, e.Integration.Spec.ServiceAccountName, e.Integration.Name, e.Integration.Namespace)
if err != nil {
return err
}
roleBinding, err := loadResource(e, "master-role-binding.tmpl", templateData)
if err != nil {
return err
// Add the RBAC privileges
e.Resources.AddAll(privileges)

if e.CamelCatalog.Runtime.Capabilities["master"].RuntimeProperties != nil {
t.setCatalogConfiguration(e)
} else {
t.setCustomizerConfiguration(e)
}
}

e.Resources.Add(role)
e.Resources.Add(roleBinding)
return nil
}

// Deprecated: to be removed in future release in favor of func setCatalogConfiguration().
func (t *masterTrait) setCustomizerConfiguration(e *trait.Environment) {
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: "customizer.master.enabled=true"},
)
if t.ResourceName != nil {
resourceName := t.ResourceName
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: "customizer.master.enabled=true"},
v1.ConfigurationSpec{Type: "property", Value: fmt.Sprintf("customizer.master.kubernetesResourceName=%s", *resourceName)},
)
}
if t.ResourceType != nil {
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: fmt.Sprintf("customizer.master.leaseResourceType=%s", *t.ResourceType)},
)
}
if t.LabelKey != nil {
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: fmt.Sprintf("customizer.master.labelKey=%s", *t.LabelKey)},
)
}
if t.LabelValue != nil {
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: fmt.Sprintf("customizer.master.labelValue=%s", *t.LabelValue)},
)
}
}

if t.ResourceName != nil {
resourceName := t.ResourceName
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: fmt.Sprintf("customizer.master.kubernetesResourceName=%s", *resourceName)},
)
}

if t.ResourceType != nil {
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: fmt.Sprintf("customizer.master.leaseResourceType=%s", *t.ResourceType)},
)
}

if t.LabelKey != nil {
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: fmt.Sprintf("customizer.master.labelKey=%s", *t.LabelKey)},
)
}

if t.LabelValue != nil {
e.Integration.Status.Configuration = append(e.Integration.Status.Configuration,
v1.ConfigurationSpec{Type: "property", Value: fmt.Sprintf("customizer.master.labelValue=%s", *t.LabelValue)},
)
}
func (t *masterTrait) setCatalogConfiguration(e *trait.Environment) {
if e.ApplicationProperties == nil {
e.ApplicationProperties = make(map[string]string)
}
if t.ResourceName != nil {
e.ApplicationProperties["camel.k.master.resourceName"] = *t.ResourceName
}
if t.ResourceType != nil {
e.ApplicationProperties["camel.k.master.resourceType"] = *t.ResourceType
}
if t.LabelKey != nil && t.LabelValue != nil {
e.ApplicationProperties["camel.k.master.labelKey"] = *t.LabelKey
e.ApplicationProperties["camel.k.master.labelValue"] = *t.LabelValue
}

return nil
for _, cp := range e.CamelCatalog.Runtime.Capabilities["master"].RuntimeProperties {
e.ApplicationProperties[trait.CapabilityPropertyKey(cp.Key, e.ApplicationProperties)] = cp.Value
}
}

func (t *masterTrait) canUseLeases(e *trait.Environment) (bool, error) {
Expand All @@ -246,14 +250,49 @@ func findAdditionalDependencies(e *trait.Environment, meta metadata.IntegrationM
return dependencies
}

func loadResource(e *trait.Environment, name string, params interface{}) (ctrl.Object, error) {
data, err := resources.TemplateResource(fmt.Sprintf("/resources/addons/master/%s", name), params)
func loadResource(cli client.Client, name string, params interface{}) (ctrl.Object, error) {
data, err := resources.TemplateResource(fmt.Sprintf("resources/addons/master/%s", name), params)
if err != nil {
return nil, err
}
obj, err := kubernetes.LoadResourceFromYaml(e.Client.GetScheme(), data)
obj, err := kubernetes.LoadResourceFromYaml(cli.GetScheme(), data)
if err != nil {
return nil, err
}
return obj, nil
}

func (t *masterTrait) prepareRBAC(cli client.Client, serviceAccount, itName, itNamespace string) ([]ctrl.Object, error) {
objs := make([]ctrl.Object, 0, 2)
if serviceAccount == "" {
serviceAccount = "default"
}

templateData := struct {
Namespace string
Name string
ServiceAccount string
}{
Namespace: itNamespace,
Name: fmt.Sprintf("%s-master", itName),
ServiceAccount: serviceAccount,
}

roleSuffix := leaseResourceType
if t.ResourceType != nil {
roleSuffix = *t.ResourceType
}
roleSuffix = strings.ToLower(roleSuffix)

role, err := loadResource(cli, fmt.Sprintf("master-role-%s.tmpl", roleSuffix), templateData)
if err != nil {
return nil, err
}
objs = append(objs, role)
roleBinding, err := loadResource(cli, "master-role-binding.tmpl", templateData)
if err != nil {
return nil, err
}
objs = append(objs, roleBinding)
return objs, nil
}
Loading

0 comments on commit e06a8cf

Please sign in to comment.