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
2126type 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
151195func (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
164211func (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
169220func (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