Skip to content

Commit 4e416be

Browse files
authored
Introduce KubernetesInstallation Type (#2367)
* Introduce KubernetesInstallation Type * generate * f * f
1 parent 3c85d28 commit 4e416be

File tree

5 files changed

+389
-0
lines changed

5 files changed

+389
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
type KubernetesInstallationState string
24+
25+
// What follows is a list of all valid states for an KubernetesInstallation object.
26+
const (
27+
KubernetesInstallationStateEnqueued KubernetesInstallationState = "Enqueued"
28+
KubernetesInstallationStateInstalling KubernetesInstallationState = "Installing"
29+
KubernetesInstallationStateInstalled KubernetesInstallationState = "Installed"
30+
KubernetesInstallationStateAddonsInstalling KubernetesInstallationState = "AddonsInstalling"
31+
KubernetesInstallationStateAddonsInstalled KubernetesInstallationState = "AddonsInstalled"
32+
KubernetesInstallationStateObsolete KubernetesInstallationState = "Obsolete"
33+
KubernetesInstallationStateFailed KubernetesInstallationState = "Failed"
34+
KubernetesInstallationStateUnknown KubernetesInstallationState = "Unknown"
35+
)
36+
37+
// KubernetesInstallationSpec defines the desired state of KubernetesInstallation.
38+
type KubernetesInstallationSpec struct {
39+
// ClusterID holds the cluster id, generated during the installation.
40+
ClusterID string `json:"clusterID,omitempty"`
41+
// MetricsBaseURL holds the base URL for the metrics server.
42+
MetricsBaseURL string `json:"metricsBaseURL,omitempty"`
43+
// Config holds the configuration used at installation time.
44+
Config *ConfigSpec `json:"config,omitempty"`
45+
// BinaryName holds the name of the binary used to install the cluster.
46+
// this will follow the pattern 'appslug-channelslug'
47+
BinaryName string `json:"binaryName,omitempty"`
48+
// LicenseInfo holds information about the license used to install the cluster.
49+
LicenseInfo *LicenseInfo `json:"licenseInfo,omitempty"`
50+
// Proxy holds the proxy configuration.
51+
Proxy *ProxySpec `json:"proxy,omitempty"`
52+
// AdminConsole holds the Admin Console configuration.
53+
AdminConsole AdminConsoleSpec `json:"adminConsole,omitempty"`
54+
// Manager holds the Manager configuration.
55+
Manager ManagerSpec `json:"manager,omitempty"`
56+
// HighAvailability indicates if the installation is high availability.
57+
HighAvailability bool `json:"highAvailability,omitempty"`
58+
// AirGap indicates if the installation is airgapped.
59+
AirGap bool `json:"airGap,omitempty"`
60+
}
61+
62+
// KubernetesInstallationStatus defines the observed state of KubernetesInstallation
63+
type KubernetesInstallationStatus struct {
64+
// State holds the current state of the installation.
65+
State KubernetesInstallationState `json:"state,omitempty"`
66+
// Reason holds the reason for the current state.
67+
Reason string `json:"reason,omitempty"`
68+
}
69+
70+
// KubernetesInstallation is the Schema for the kubernetes installations API
71+
type KubernetesInstallation struct {
72+
metav1.TypeMeta `json:",inline"`
73+
metav1.ObjectMeta `json:"metadata,omitempty"`
74+
75+
Spec KubernetesInstallationSpec `json:"spec,omitempty"`
76+
Status KubernetesInstallationStatus `json:"status,omitempty"`
77+
}
78+
79+
func GetDefaultKubernetesInstallationSpec() KubernetesInstallationSpec {
80+
c := KubernetesInstallationSpec{}
81+
kubernetesInstallationSpecSetDefaults(&c)
82+
return c
83+
}
84+
85+
func kubernetesInstallationSpecSetDefaults(c *KubernetesInstallationSpec) {
86+
adminConsoleSpecSetDefaults(&c.AdminConsole)
87+
managerSpecSetDefaults(&c.Manager)
88+
}

kinds/apis/v1beta1/zz_generated.deepcopy.go

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package kubernetesinstallation
2+
3+
import (
4+
"os"
5+
6+
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
7+
)
8+
9+
var _ Installation = &kubernetesInstallation{}
10+
11+
type Option func(*kubernetesInstallation)
12+
13+
type EnvSetter interface {
14+
Setenv(key string, val string) error
15+
}
16+
17+
type kubernetesInstallation struct {
18+
installation *ecv1beta1.KubernetesInstallation
19+
envSetter EnvSetter
20+
}
21+
22+
type osEnvSetter struct{}
23+
24+
func (o *osEnvSetter) Setenv(key string, val string) error {
25+
return os.Setenv(key, val)
26+
}
27+
28+
func WithEnvSetter(envSetter EnvSetter) Option {
29+
return func(rc *kubernetesInstallation) {
30+
rc.envSetter = envSetter
31+
}
32+
}
33+
34+
// New creates a new KubernetesInstallation instance
35+
func New(installation *ecv1beta1.KubernetesInstallation, opts ...Option) Installation {
36+
if installation == nil {
37+
installation = &ecv1beta1.KubernetesInstallation{
38+
Spec: ecv1beta1.GetDefaultKubernetesInstallationSpec(),
39+
}
40+
}
41+
42+
ki := &kubernetesInstallation{installation: installation}
43+
for _, opt := range opts {
44+
opt(ki)
45+
}
46+
47+
if ki.envSetter == nil {
48+
ki.envSetter = &osEnvSetter{}
49+
}
50+
51+
return ki
52+
}
53+
54+
// Get returns the KubernetesInstallation.
55+
func (ki *kubernetesInstallation) Get() *ecv1beta1.KubernetesInstallation {
56+
return ki.installation
57+
}
58+
59+
// Set sets the KubernetesInstallation.
60+
func (ki *kubernetesInstallation) Set(installation *ecv1beta1.KubernetesInstallation) {
61+
if installation == nil {
62+
return
63+
}
64+
ki.installation = installation
65+
}
66+
67+
// GetSpec returns the spec for the KubernetesInstallation.
68+
func (ki *kubernetesInstallation) GetSpec() ecv1beta1.KubernetesInstallationSpec {
69+
return ki.installation.Spec
70+
}
71+
72+
// SetSpec sets the spec for the KubernetesInstallation.
73+
func (ki *kubernetesInstallation) SetSpec(spec ecv1beta1.KubernetesInstallationSpec) {
74+
ki.installation.Spec = spec
75+
}
76+
77+
// GetStatus returns the status for the KubernetesInstallation.
78+
func (ki *kubernetesInstallation) GetStatus() ecv1beta1.KubernetesInstallationStatus {
79+
return ki.installation.Status
80+
}
81+
82+
// SetStatus sets the status for the KubernetesInstallation.
83+
func (ki *kubernetesInstallation) SetStatus(status ecv1beta1.KubernetesInstallationStatus) {
84+
ki.installation.Status = status
85+
}
86+
87+
// SetEnv sets the environment variables for the KubernetesInstallation.
88+
func (ki *kubernetesInstallation) SetEnv() error {
89+
return nil
90+
}
91+
92+
// AdminConsolePort returns the configured port for the admin console or the default if not
93+
// configured.
94+
func (ki *kubernetesInstallation) AdminConsolePort() int {
95+
if ki.installation.Spec.AdminConsole.Port > 0 {
96+
return ki.installation.Spec.AdminConsole.Port
97+
}
98+
return ecv1beta1.DefaultAdminConsolePort
99+
}
100+
101+
// ManagerPort returns the configured port for the manager or the default if not
102+
// configured.
103+
func (ki *kubernetesInstallation) ManagerPort() int {
104+
if ki.installation.Spec.Manager.Port > 0 {
105+
return ki.installation.Spec.Manager.Port
106+
}
107+
return ecv1beta1.DefaultManagerPort
108+
}
109+
110+
// ProxySpec returns the configured proxy spec or nil if not configured.
111+
func (ki *kubernetesInstallation) ProxySpec() *ecv1beta1.ProxySpec {
112+
return ki.installation.Spec.Proxy
113+
}
114+
115+
// SetAdminConsolePort sets the port for the admin console.
116+
func (ki *kubernetesInstallation) SetAdminConsolePort(port int) {
117+
ki.installation.Spec.AdminConsole.Port = port
118+
}
119+
120+
// SetManagerPort sets the port for the manager.
121+
func (ki *kubernetesInstallation) SetManagerPort(port int) {
122+
ki.installation.Spec.Manager.Port = port
123+
}
124+
125+
// SetProxySpec sets the proxy spec for the kubernetes installation.
126+
func (ki *kubernetesInstallation) SetProxySpec(proxySpec *ecv1beta1.ProxySpec) {
127+
ki.installation.Spec.Proxy = proxySpec
128+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package kubernetesinstallation
2+
3+
import (
4+
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
5+
)
6+
7+
// Installation defines the interface for managing kubernetes installation
8+
type Installation interface {
9+
Get() *ecv1beta1.KubernetesInstallation
10+
Set(installation *ecv1beta1.KubernetesInstallation)
11+
12+
GetSpec() ecv1beta1.KubernetesInstallationSpec
13+
SetSpec(spec ecv1beta1.KubernetesInstallationSpec)
14+
15+
GetStatus() ecv1beta1.KubernetesInstallationStatus
16+
SetStatus(status ecv1beta1.KubernetesInstallationStatus)
17+
18+
AdminConsolePort() int
19+
ManagerPort() int
20+
ProxySpec() *ecv1beta1.ProxySpec
21+
22+
SetAdminConsolePort(port int)
23+
SetManagerPort(port int)
24+
SetProxySpec(proxySpec *ecv1beta1.ProxySpec)
25+
}

pkg/kubernetesinstallation/mock.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package kubernetesinstallation
2+
3+
import (
4+
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
5+
"github.com/stretchr/testify/mock"
6+
)
7+
8+
var _ Installation = (*MockInstallation)(nil)
9+
10+
// MockInstallation is a mock implementation of the KubernetesInstallation interface
11+
type MockInstallation struct {
12+
mock.Mock
13+
}
14+
15+
// Get mocks the Get method
16+
func (m *MockInstallation) Get() *ecv1beta1.KubernetesInstallation {
17+
args := m.Called()
18+
if args.Get(0) == nil {
19+
return nil
20+
}
21+
return args.Get(0).(*ecv1beta1.KubernetesInstallation)
22+
}
23+
24+
// Set mocks the Set method
25+
func (m *MockInstallation) Set(installation *ecv1beta1.KubernetesInstallation) {
26+
m.Called(installation)
27+
}
28+
29+
// GetSpec mocks the GetSpec method
30+
func (m *MockInstallation) GetSpec() ecv1beta1.KubernetesInstallationSpec {
31+
args := m.Called()
32+
return args.Get(0).(ecv1beta1.KubernetesInstallationSpec)
33+
}
34+
35+
// SetSpec mocks the SetSpec method
36+
func (m *MockInstallation) SetSpec(spec ecv1beta1.KubernetesInstallationSpec) {
37+
m.Called(spec)
38+
}
39+
40+
// GetStatus mocks the GetStatus method
41+
func (m *MockInstallation) GetStatus() ecv1beta1.KubernetesInstallationStatus {
42+
args := m.Called()
43+
return args.Get(0).(ecv1beta1.KubernetesInstallationStatus)
44+
}
45+
46+
// SetStatus mocks the SetStatus method
47+
func (m *MockInstallation) SetStatus(status ecv1beta1.KubernetesInstallationStatus) {
48+
m.Called(status)
49+
}
50+
51+
// AdminConsolePort mocks the AdminConsolePort method
52+
func (m *MockInstallation) AdminConsolePort() int {
53+
args := m.Called()
54+
return args.Int(0)
55+
}
56+
57+
// ManagerPort mocks the ManagerPort method
58+
func (m *MockInstallation) ManagerPort() int {
59+
args := m.Called()
60+
return args.Int(0)
61+
}
62+
63+
// ProxySpec mocks the ProxySpec method
64+
func (m *MockInstallation) ProxySpec() *ecv1beta1.ProxySpec {
65+
args := m.Called()
66+
return args.Get(0).(*ecv1beta1.ProxySpec)
67+
}
68+
69+
// SetAdminConsolePort mocks the SetAdminConsolePort method
70+
func (m *MockInstallation) SetAdminConsolePort(port int) {
71+
m.Called(port)
72+
}
73+
74+
// SetManagerPort mocks the SetManagerPort method
75+
func (m *MockInstallation) SetManagerPort(port int) {
76+
m.Called(port)
77+
}
78+
79+
// SetProxySpec mocks the SetProxySpec method
80+
func (m *MockInstallation) SetProxySpec(proxySpec *ecv1beta1.ProxySpec) {
81+
m.Called(proxySpec)
82+
}

0 commit comments

Comments
 (0)