Skip to content

Commit 1dcd452

Browse files
authored
Merge pull request #31 from pluralsh/use-global-settings
use global settings for BYOK cluster, upsert namespace
2 parents ffa21b4 + 51a5ee8 commit 1dcd452

File tree

5 files changed

+77
-13
lines changed

5 files changed

+77
-13
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
1313
github.com/hashicorp/terraform-plugin-log v0.9.0
1414
github.com/mitchellh/go-homedir v1.1.0
15-
github.com/pluralsh/console-client-go v0.1.16
15+
github.com/pluralsh/console-client-go v0.1.17
1616
github.com/pluralsh/plural-cli v0.8.5-0.20240216094552-efc34ee6de37
1717
github.com/pluralsh/polly v0.1.7
1818
github.com/samber/lo v1.38.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
856856
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
857857
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
858858
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
859-
github.com/pluralsh/console-client-go v0.1.16 h1:f+d4ah3r+dAJ6hSMFsAmTlps4IsmExCzkCOwUpSYkbs=
860-
github.com/pluralsh/console-client-go v0.1.16/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
859+
github.com/pluralsh/console-client-go v0.1.17 h1:QMtnWdRvV13/sND/CFjFBUR8nyg3JJgwXReSyM6bK7A=
860+
github.com/pluralsh/console-client-go v0.1.17/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
861861
github.com/pluralsh/gqlclient v1.11.0 h1:FfXW7FiEJLHOfTAa7NxDb8jb3aMZNIpCAcG+bg8uHYA=
862862
github.com/pluralsh/gqlclient v1.11.0/go.mod h1:qSXKUlio1F2DRPy8el4oFYsmpKbkUYspgPB87T4it5I=
863863
github.com/pluralsh/plural-cli v0.8.5-0.20240216094552-efc34ee6de37 h1:DBnaKvKmbTbKwbkrh/2gJBwyHYfaXdxeT3UGh+94K4g=

internal/resource/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
7171
return
7272
}
7373

74-
handler, err := NewOperatorHandler(ctx, data.GetKubeconfig(), data.HelmRepoUrl.ValueString(), data.HelmValues.ValueStringPointer(), r.consoleUrl)
74+
handler, err := NewOperatorHandler(ctx, r.client, data.GetKubeconfig(), data.HelmRepoUrl.ValueString(), data.HelmValues.ValueStringPointer(), r.consoleUrl)
7575
if err != nil {
7676
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to init operator handler, got error: %s", err))
7777
return

internal/resource/cluster_kubeconfig.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
"time"
99

1010
"github.com/hashicorp/terraform-plugin-framework/types"
11-
"github.com/pluralsh/polly/algorithms"
12-
"github.com/samber/lo"
13-
"k8s.io/client-go/discovery/cached/disk"
14-
1511
"github.com/hashicorp/terraform-plugin-log/tflog"
1612
"github.com/mitchellh/go-homedir"
13+
"github.com/pluralsh/polly/algorithms"
14+
"github.com/samber/lo"
1715
"k8s.io/apimachinery/pkg/api/meta"
1816
apimachineryschema "k8s.io/apimachinery/pkg/runtime/schema"
1917
"k8s.io/client-go/discovery"
18+
"k8s.io/client-go/discovery/cached/disk"
19+
"k8s.io/client-go/kubernetes"
2020
"k8s.io/client-go/rest"
2121
"k8s.io/client-go/restmapper"
2222
"k8s.io/client-go/tools/clientcmd"
@@ -27,6 +27,14 @@ type KubeConfig struct {
2727
ClientConfig clientcmd.ClientConfig
2828
}
2929

30+
func (k *KubeConfig) ToClientSet() (*kubernetes.Clientset, error) {
31+
config, err := k.ToRawKubeConfigLoader().ClientConfig()
32+
if err != nil {
33+
return nil, err
34+
}
35+
return kubernetes.NewForConfig(config)
36+
}
37+
3038
func (k *KubeConfig) ToRESTConfig() (*rest.Config, error) {
3139
return k.ToRawKubeConfigLoader().ClientConfig()
3240
}

internal/resource/cluster_operator_handler.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"terraform-provider-plural/internal/client"
9+
810
"github.com/pluralsh/plural-cli/pkg/console"
911
"github.com/pluralsh/plural-cli/pkg/helm"
1012
"github.com/pluralsh/polly/algorithms"
@@ -15,10 +17,17 @@ import (
1517
"helm.sh/helm/v3/pkg/chart/loader"
1618
"helm.sh/helm/v3/pkg/cli"
1719
"helm.sh/helm/v3/pkg/release"
20+
v1 "k8s.io/api/core/v1"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"k8s.io/client-go/kubernetes"
1823
"sigs.k8s.io/yaml"
1924
)
2025

2126
type OperatorHandler struct {
27+
client *client.Client
28+
29+
kube *kubernetes.Clientset
30+
2231
ctx context.Context
2332

2433
// kubeconfig is a model.Kubeconfig data model read from terraform
@@ -48,6 +57,11 @@ func (oh *OperatorHandler) init() error {
4857
if err != nil {
4958
return err
5059
}
60+
kube, err := kubeconfig.ToClientSet()
61+
if err != nil {
62+
return err
63+
}
64+
oh.kube = kube
5165

5266
err = oh.configuration.Init(kubeconfig, console.OperatorNamespace, "", logrus.Debugf)
5367
if err != nil {
@@ -138,17 +152,50 @@ func (oh *OperatorHandler) listReleases(state action.ListStates) ([]*release.Rel
138152
return client.Run()
139153
}
140154

141-
func (oh *OperatorHandler) values(token string) map[string]interface{} {
155+
func (oh *OperatorHandler) values(token string) (map[string]interface{}, error) {
156+
globalVals := map[string]interface{}{}
142157
vals := map[string]interface{}{
143158
"secrets": map[string]string{
144159
"deployToken": token,
145160
},
146161
"consoleUrl": fmt.Sprintf("%s/ext/gql", oh.url),
147162
}
148-
return algorithms.Merge(vals, oh.vals)
163+
164+
setting, err := oh.client.GetDeploymentSettings(oh.ctx)
165+
if err != nil {
166+
return nil, err
167+
}
168+
if setting != nil && setting.DeploymentSettings != nil && setting.DeploymentSettings.AgentHelmValues != nil {
169+
if err := yaml.Unmarshal([]byte(*setting.DeploymentSettings.AgentHelmValues), &globalVals); err != nil {
170+
return nil, err
171+
}
172+
}
173+
return algorithms.Merge(vals, oh.vals, globalVals), nil
174+
}
175+
176+
func (oh *OperatorHandler) UpsertNamespace() error {
177+
_, err := oh.kube.CoreV1().Namespaces().Get(oh.ctx, console.OperatorNamespace, metav1.GetOptions{})
178+
if err == nil {
179+
return nil
180+
}
181+
182+
_, err = oh.kube.CoreV1().Namespaces().Create(oh.ctx, &v1.Namespace{
183+
ObjectMeta: metav1.ObjectMeta{
184+
Name: console.OperatorNamespace,
185+
Labels: map[string]string{
186+
"app.kubernetes.io/managed-by": "plural",
187+
"app.plural.sh/name": console.OperatorNamespace,
188+
},
189+
},
190+
}, metav1.CreateOptions{})
191+
192+
return err
149193
}
150194

151195
func (oh *OperatorHandler) InstallOrUpgrade(token string) error {
196+
if err := oh.UpsertNamespace(); err != nil {
197+
return err
198+
}
152199
exists, err := oh.chartExists()
153200
if err != nil {
154201
return err
@@ -162,12 +209,20 @@ func (oh *OperatorHandler) InstallOrUpgrade(token string) error {
162209
}
163210

164211
func (oh *OperatorHandler) Install(token string) error {
165-
_, err := oh.install.Run(oh.chart, oh.values(token))
212+
values, err := oh.values(token)
213+
if err != nil {
214+
return err
215+
}
216+
_, err = oh.install.Run(oh.chart, values)
166217
return err
167218
}
168219

169220
func (oh *OperatorHandler) Upgrade(token string) error {
170-
_, err := oh.upgrade.Run(console.ReleaseName, oh.chart, oh.values(token))
221+
values, err := oh.values(token)
222+
if err != nil {
223+
return err
224+
}
225+
_, err = oh.upgrade.Run(console.ReleaseName, oh.chart, values)
171226
return err
172227
}
173228

@@ -176,7 +231,7 @@ func (oh *OperatorHandler) Uninstall() error {
176231
return err
177232
}
178233

179-
func NewOperatorHandler(ctx context.Context, kubeconfig *Kubeconfig, repoUrl string, values *string, consoleUrl string) (*OperatorHandler, error) {
234+
func NewOperatorHandler(ctx context.Context, client *client.Client, kubeconfig *Kubeconfig, repoUrl string, values *string, consoleUrl string) (*OperatorHandler, error) {
180235
vals := map[string]interface{}{}
181236
if values != nil {
182237
if err := yaml.Unmarshal([]byte(*values), &vals); err != nil {
@@ -185,6 +240,7 @@ func NewOperatorHandler(ctx context.Context, kubeconfig *Kubeconfig, repoUrl str
185240
}
186241

187242
handler := &OperatorHandler{
243+
client: client,
188244
ctx: ctx,
189245
kubeconfig: kubeconfig,
190246
repoUrl: repoUrl,

0 commit comments

Comments
 (0)