/
actuator_reconcile.go
119 lines (102 loc) · 4.34 KB
/
actuator_reconcile.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
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed 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 infrastructure
import (
"context"
"fmt"
"path/filepath"
packetv1alpha1 "github.com/gardener/gardener-extension-provider-packet/pkg/apis/packet/v1alpha1"
"github.com/gardener/gardener-extension-provider-packet/pkg/packet"
extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
"github.com/gardener/gardener/extensions/pkg/terraformer"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/retry"
)
func (a *actuator) Reconcile(ctx context.Context, infrastructure *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster) error {
return a.reconcile(ctx, infrastructure, cluster, terraformer.StateConfigMapInitializerFunc(terraformer.CreateState))
}
func (a *actuator) reconcile(ctx context.Context, infrastructure *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster, stateInitializer terraformer.StateConfigMapInitializer) error {
credentials, err := packet.GetCredentialsFromSecretRef(ctx, a.Client(), infrastructure.Spec.SecretRef)
if err != nil {
return err
}
terraformConfig := GenerateTerraformInfraConfig(infrastructure, string(credentials.ProjectID))
release, err := a.ChartRenderer().Render(filepath.Join(packet.InternalChartsPath, "packet-infra"), "packet-infra", infrastructure.Namespace, terraformConfig)
if err != nil {
return fmt.Errorf("could not render Terraform chart: %+v", err)
}
tf, err := a.newTerraformer(packet.TerraformerPurposeInfra, infrastructure.Namespace, infrastructure.Name)
if err != nil {
return fmt.Errorf("could not create terraformer object: %+v", err)
}
if err := tf.
SetVariablesEnvironment(generateTerraformInfraVariablesEnvironment(credentials)).
InitializeWith(terraformer.DefaultInitializer(
a.Client(),
release.FileContent("main.tf"),
release.FileContent("variables.tf"),
[]byte(release.FileContent("terraform.tfvars")),
stateInitializer,
)).
Apply(); err != nil {
return errors.Wrap(err, "failed to apply the terraform config")
}
return a.updateProviderStatus(ctx, tf, infrastructure)
}
// GenerateTerraformInfraConfig generates the Packet Terraform configuration based on the given infrastructure and project.
func GenerateTerraformInfraConfig(infrastructure *extensionsv1alpha1.Infrastructure, projectID string) map[string]interface{} {
return map[string]interface{}{
"packet": map[string]interface{}{
"projectID": projectID,
},
"sshPublicKey": string(infrastructure.Spec.SSHPublicKey),
"clusterName": infrastructure.Namespace,
"outputKeys": map[string]interface{}{
"sshKeyID": packet.SSHKeyID,
},
}
}
func (a *actuator) updateProviderStatus(ctx context.Context, tf terraformer.Terraformer, infrastructure *extensionsv1alpha1.Infrastructure) error {
outputVarKeys := []string{
packet.SSHKeyID,
}
output, err := tf.GetStateOutputVariables(outputVarKeys...)
if err != nil {
return err
}
state, err := tf.GetRawState(ctx)
if err != nil {
return err
}
stateByte, err := state.Marshal()
if err != nil {
return err
}
return extensionscontroller.TryUpdateStatus(ctx, retry.DefaultBackoff, a.Client(), infrastructure, func() error {
infrastructure.Status.ProviderStatus = &runtime.RawExtension{
Object: &packetv1alpha1.InfrastructureStatus{
TypeMeta: metav1.TypeMeta{
APIVersion: packetv1alpha1.SchemeGroupVersion.String(),
Kind: "InfrastructureStatus",
},
SSHKeyID: output[packet.SSHKeyID],
},
}
infrastructure.Status.State = &runtime.RawExtension{Raw: stateByte}
return nil
})
}