Skip to content
This repository has been archived by the owner on Apr 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #476 from vlvasilev/put_state_in_status
Browse files Browse the repository at this point in the history
Put Infrastructure state into status as runtime.RawExtension
  • Loading branch information
rfranzke authored Dec 16, 2019
2 parents 64fce75 + 99ce9cc commit 292f8b6
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,12 @@ func (a *actuator) newInitializer(infra *extensionsv1alpha1.Infrastructure, conf
return nil, err
}

return a.terraformerFactory.DefaultInitializer(a.client, files.Main, files.Variables, files.TFVars), nil
terraformState, err := terraformer.UnmarshalRawState(infra.Status.State)
if err != nil {
return nil, err
}

return a.terraformerFactory.DefaultInitializer(a.client, files.Main, files.Variables, files.TFVars, terraformState.Data), nil
}

func (a *actuator) newTerraformer(infra *extensionsv1alpha1.Infrastructure, credentials *alicloud.Credentials) (terraformer.Terraformer, error) {
Expand Down Expand Up @@ -438,8 +443,18 @@ func (a *actuator) Reconcile(ctx context.Context, infra *extensionsv1alpha1.Infr
return err
}

state, err := tf.GetRawState(ctx)
if err != nil {
return err
}
stateByte, err := state.Marshal()
if err != nil {
return err
}

return extensioncontroller.TryUpdateStatus(ctx, retry.DefaultBackoff, a.client, infra, func() error {
infra.Status.ProviderStatus = &runtime.RawExtension{Object: status}
infra.Status.State = &runtime.RawExtension{Raw: stateByte}
return nil
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
mockterraformer "github.com/gardener/gardener-extensions/pkg/mock/gardener-extensions/terraformer"
mockgardenerchartrenderer "github.com/gardener/gardener-extensions/pkg/mock/gardener/chartrenderer"
"github.com/gardener/gardener-extensions/pkg/mock/go-logr/logr"
realterraformer "github.com/gardener/gardener-extensions/pkg/terraformer"
"github.com/gardener/gardener-extensions/pkg/util/chart"
gardencorev1alpha1 "github.com/gardener/gardener/pkg/apis/core/v1alpha1"

Expand Down Expand Up @@ -168,6 +169,10 @@ var _ = Describe("Actuator", func() {
natGatewayID = "natGatewayID"
securityGroupID = "sgID"
keyPairName = "keyPairName"
rawState = &realterraformer.RawState{
Data: "",
Encoding: "none",
}
)

describeNATGatewaysReq := vpc.CreateDescribeNatGatewaysRequest()
Expand Down Expand Up @@ -228,7 +233,7 @@ var _ = Describe("Actuator", func() {
},
}, nil),

terraformerFactory.EXPECT().DefaultInitializer(c, mainContent, variablesContent, []byte(tfVarsContent)).Return(initializer),
terraformerFactory.EXPECT().DefaultInitializer(c, mainContent, variablesContent, []byte(tfVarsContent), "").Return(initializer),

terraformer.EXPECT().InitializeWith(initializer).Return(terraformer),

Expand All @@ -255,7 +260,7 @@ var _ = Describe("Actuator", func() {
TerraformerOutputKeySecurityGroupID: securityGroupID,
TerraformerOutputKeyKeyPairName: keyPairName,
}, nil),

terraformer.EXPECT().GetRawState(context.TODO()).Return(rawState, nil),
c.EXPECT().Status().Return(c),
c.EXPECT().Get(ctx, client.ObjectKey{Namespace: infra.Namespace, Name: infra.Name}, &infra),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func (a *actuator) reconcile(ctx context.Context, infrastructure *extensionsv1al
return fmt.Errorf("failed to generate Terraform config: %+v", err)
}

terraformState, err := terraformer.UnmarshalRawState(infrastructure.Status.State)
if err != nil {
return err
}

chartRenderer, err := chartrenderer.NewForConfig(a.restConfig)
if err != nil {
return fmt.Errorf("could not create chart renderer: %+v", err)
Expand All @@ -77,7 +82,8 @@ func (a *actuator) reconcile(ctx context.Context, infrastructure *extensionsv1al
a.client,
release.FileContent("main.tf"),
release.FileContent("variables.tf"),
[]byte(release.FileContent("terraform.tfvars"))),
[]byte(release.FileContent("terraform.tfvars")),
terraformState.Data),
).
Apply(); err != nil {

Expand Down Expand Up @@ -180,6 +186,15 @@ func (a *actuator) updateProviderStatus(ctx context.Context, tf terraformer.Terr
return err
}

state, err := tf.GetRawState(ctx)
if err != nil {
return err
}
stateByte, err := state.Marshal()
if err != nil {
return err
}

subnets, err := computeProviderStatusSubnets(infrastructureConfig, output)
if err != nil {
return err
Expand Down Expand Up @@ -221,6 +236,7 @@ func (a *actuator) updateProviderStatus(ctx context.Context, tf terraformer.Terr
},
},
}
infrastructure.Status.State = &runtime.RawExtension{Raw: stateByte}
return nil
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package infrastructure

import (
"context"

azurev1alpha1 "github.com/gardener/gardener-extensions/controllers/provider-azure/pkg/apis/azure/v1alpha1"
infrainternal "github.com/gardener/gardener-extensions/controllers/provider-azure/pkg/internal/infrastructure"
extensionscontroller "github.com/gardener/gardener-extensions/pkg/controller"
Expand Down Expand Up @@ -79,8 +78,18 @@ func (a *actuator) updateProviderStatus(
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, infra, func() error {
infra.Status.ProviderStatus = &runtime.RawExtension{Object: status}
infra.Status.State = &runtime.RawExtension{Raw: stateByte}
return nil
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (a *actuator) Reconcile(ctx context.Context, infra *extensionsv1alpha1.Infr
return err
}

terraformState, err := terraformer.UnmarshalRawState(infra.Status.State)
if err != nil {
return err
}

terraformFiles, err := infrastructure.RenderTerraformerChart(a.chartRenderer, infra, clientAuth, config, cluster)
if err != nil {
return err
Expand All @@ -50,7 +55,7 @@ func (a *actuator) Reconcile(ctx context.Context, infra *extensionsv1alpha1.Infr
}

if err := tf.
InitializeWith(terraformer.DefaultInitializer(a.client, terraformFiles.Main, terraformFiles.Variables, terraformFiles.TFVars)).
InitializeWith(terraformer.DefaultInitializer(a.client, terraformFiles.Main, terraformFiles.Variables, terraformFiles.TFVars, terraformState.Data)).
Apply(); err != nil {

a.logger.Error(err, "failed to apply the terraform config", "infrastructure", infra.Name)
Expand Down
10 changes: 10 additions & 0 deletions controllers/provider-gcp/pkg/controller/infrastructure/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,18 @@ func (a *actuator) updateProviderStatus(
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, infra, func() error {
infra.Status.ProviderStatus = &runtime.RawExtension{Object: status}
infra.Status.State = &runtime.RawExtension{Raw: stateByte}
return nil
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (a *actuator) Reconcile(ctx context.Context, infra *extensionsv1alpha1.Infr
return err
}

terraformState, err := terraformer.UnmarshalRawState(infra.Status.State)
if err != nil {
return err
}

terraformFiles, err := infrastructure.RenderTerraformerChart(a.chartRenderer, infra, serviceAccount, config, cluster)
if err != nil {
return err
Expand All @@ -50,7 +55,7 @@ func (a *actuator) Reconcile(ctx context.Context, infra *extensionsv1alpha1.Infr
}

if err := tf.
InitializeWith(terraformer.DefaultInitializer(a.client, terraformFiles.Main, terraformFiles.Variables, terraformFiles.TFVars)).
InitializeWith(terraformer.DefaultInitializer(a.client, terraformFiles.Main, terraformFiles.Variables, terraformFiles.TFVars, terraformState.Data)).
Apply(); err != nil {

a.logger.Error(err, "failed to apply the terraform config", "infrastructure", infra.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package infrastructure

import (
"context"

"github.com/gardener/gardener/pkg/chartrenderer"
"k8s.io/client-go/util/retry"

Expand Down Expand Up @@ -101,8 +102,18 @@ func (a *actuator) updateProviderStatus(
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, infra, func() error {
infra.Status.ProviderStatus = &runtime.RawExtension{Object: status}
infra.Status.State = &runtime.RawExtension{Raw: stateByte}
return nil
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (a *actuator) reconcile(ctx context.Context, infra *extensionsv1alpha1.Infr
return err
}

terraformState, err := terraformer.UnmarshalRawState(infra.Status.State)
if err != nil {
return err
}

terraformFiles, err := infrastructure.RenderTerraformerChart(a.chartRenderer, infra, creds, config, cluster)
if err != nil {
return err
Expand All @@ -49,7 +54,7 @@ func (a *actuator) reconcile(ctx context.Context, infra *extensionsv1alpha1.Infr
}

if err := tf.
InitializeWith(terraformer.DefaultInitializer(a.client, terraformFiles.Main, terraformFiles.Variables, terraformFiles.TFVars)).
InitializeWith(terraformer.DefaultInitializer(a.client, terraformFiles.Main, terraformFiles.Variables, terraformFiles.TFVars, terraformState.Data)).
Apply(); err != nil {

a.logger.Error(err, "failed to apply the terraform config", "infrastructure", infra.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (a *actuator) reconcile(ctx context.Context, infrastructure *extensionsv1al

terraformConfig := GenerateTerraformInfraConfig(infrastructure, string(providerSecret.Data[packet.ProjectID]))

terraformState, err := terraformer.UnmarshalRawState(infrastructure.Status.State)
if err != nil {
return fmt.Errorf("could not retrieve raw terraform state: %+v", err)
}

chartRenderer, err := chartrenderer.NewForConfig(a.restConfig)
if err != nil {
return fmt.Errorf("could not create chart renderer: %+v", err)
Expand All @@ -64,7 +69,8 @@ func (a *actuator) reconcile(ctx context.Context, infrastructure *extensionsv1al
a.client,
release.FileContent("main.tf"),
release.FileContent("variables.tf"),
[]byte(release.FileContent("terraform.tfvars"))),
[]byte(release.FileContent("terraform.tfvars")),
terraformState.Data),
).
Apply(); err != nil {

Expand Down Expand Up @@ -102,6 +108,15 @@ func (a *actuator) updateProviderStatus(ctx context.Context, tf terraformer.Terr
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{
Expand All @@ -112,6 +127,7 @@ func (a *actuator) updateProviderStatus(ctx context.Context, tf terraformer.Terr
SSHKeyID: output[packet.SSHKeyID],
},
}
infrastructure.Status.State = &runtime.RawExtension{Raw: stateByte}
return nil
})
}
24 changes: 20 additions & 4 deletions pkg/mock/gardener-extensions/terraformer/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions pkg/terraformer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ func (t *terraformer) InitializeWith(initializer Initializer) Terraformer {
return t
}

//GetRawState returns the conten of terraform state config map
func (t *terraformer) GetRawState(ctx context.Context) (*RawState, error) {
configMap := &corev1.ConfigMap{}
if err := t.client.Get(ctx, kutil.Key(t.namespace, t.stateName), configMap); err != nil {
return nil, err
}
return &RawState{
Data: configMap.Data[StateKey],
Encoding: NoneEncoding,
}, nil
}

func createOrUpdateConfigMap(ctx context.Context, c client.Client, namespace, name string, values map[string]string) (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name}}
return configMap, kutil.CreateOrUpdate(ctx, c, configMap, func() error {
Expand Down Expand Up @@ -156,8 +168,8 @@ func (f initializerFunc) Initialize(config *InitializerConfig) error {
}

// DefaultInitializer is an Initializer that initializes the configuration, variables and state resources
// based on the given main, variables and tfvars content and on the given InitializerConfig.
func DefaultInitializer(c client.Client, main, variables string, tfvars []byte) Initializer {
// based on the given main, variables, tfvars and state content and on the given InitializerConfig.
func DefaultInitializer(c client.Client, main, variables string, tfvars []byte, state string) Initializer {
return initializerFunc(func(config *InitializerConfig) error {
ctx := context.TODO()
if _, err := CreateOrUpdateConfigurationConfigMap(ctx, c, config.Namespace, config.ConfigurationName, main, variables); err != nil {
Expand All @@ -169,7 +181,7 @@ func DefaultInitializer(c client.Client, main, variables string, tfvars []byte)
}

if config.InitializeState {
if err := CreateStateConfigMap(ctx, c, config.Namespace, config.StateName, ""); err != nil && !apierrors.IsAlreadyExists(err) {
if err := CreateStateConfigMap(ctx, c, config.Namespace, config.StateName, state); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
}
Expand Down
Loading

0 comments on commit 292f8b6

Please sign in to comment.