/
platform.go
138 lines (120 loc) · 4.51 KB
/
platform.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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 trait
import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/util/openshift"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
)
// The platform trait is a base trait that is used to assign an integration platform to an integration.
//
// In case the platform is missing, the trait is allowed to create a default platform.
// This feature is especially useful in contexts where there's no need to provide a custom configuration for the platform
// (e.g. on OpenShift the default settings work, since there's an embedded container image registry).
//
// +camel-k:trait=platform
type platformTrait struct {
BaseTrait `property:",squash"`
// To create a default (empty) platform when the platform is missing.
CreateDefault *bool `property:"create-default" json:"createDefault,omitempty"`
// Indicates if the platform should be created globally in the case of global operator (default true).
Global *bool `property:"global" json:"global,omitempty"`
// To automatically detect from the environment if a default platform can be created (it will be created on OpenShift only).
Auto *bool `property:"auto" json:"auto,omitempty"`
}
func newPlatformTrait() Trait {
return &platformTrait{
BaseTrait: NewBaseTrait("platform", 100),
}
}
func (t *platformTrait) Configure(e *Environment) (bool, error) {
if IsFalse(t.Enabled) {
return false, nil
}
if !e.IntegrationInPhase(v1.IntegrationPhaseNone, v1.IntegrationPhaseWaitingForPlatform) {
return false, nil
}
if IsNilOrFalse(t.Auto) {
if e.Platform == nil {
if t.CreateDefault == nil {
// Calculate if the platform should be automatically created when missing.
if ocp, err := openshift.IsOpenShift(t.Client); err != nil {
return false, err
} else if ocp {
t.CreateDefault = &ocp
}
}
if t.Global == nil {
globalOperator := platform.IsCurrentOperatorGlobal()
t.Global = &globalOperator
}
}
}
return true, nil
}
func (t *platformTrait) Apply(e *Environment) error {
initial := e.Integration.DeepCopy()
pl, err := t.getOrCreatePlatform(e)
if err != nil || pl.Status.Phase != v1.IntegrationPlatformPhaseReady {
e.Integration.Status.Phase = v1.IntegrationPhaseWaitingForPlatform
} else {
e.Integration.Status.Phase = v1.IntegrationPhaseInitialization
}
if initial.Status.Phase != e.Integration.Status.Phase {
if err != nil {
e.Integration.Status.SetErrorCondition(v1.IntegrationConditionPlatformAvailable, v1.IntegrationConditionPlatformAvailableReason, err)
}
if pl != nil {
e.Integration.SetIntegrationPlatform(pl)
}
}
return nil
}
func (t *platformTrait) getOrCreatePlatform(e *Environment) (*v1.IntegrationPlatform, error) {
pl, err := platform.GetOrFind(t.Ctx, t.Client, e.Integration.Namespace, e.Integration.Status.Platform, false)
if err != nil && k8serrors.IsNotFound(err) {
if IsTrue(t.CreateDefault) {
platformName := e.Integration.Status.Platform
if platformName == "" {
platformName = platform.DefaultPlatformName
}
namespace := e.Integration.Namespace
if IsTrue(t.Global) {
operatorNamespace := platform.GetOperatorNamespace()
if operatorNamespace != "" {
namespace = operatorNamespace
}
}
defaultPlatform := v1.NewIntegrationPlatform(namespace, platformName)
if defaultPlatform.Labels == nil {
defaultPlatform.Labels = make(map[string]string)
}
defaultPlatform.Labels["camel.apache.org/platform.generated"] = True
pl = &defaultPlatform
e.Resources.Add(pl)
return pl, nil
}
}
return pl, err
}
// IsPlatformTrait overrides base class method
func (t *platformTrait) IsPlatformTrait() bool {
return true
}
// RequiresIntegrationPlatform overrides base class method
func (t *platformTrait) RequiresIntegrationPlatform() bool {
return false
}