Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flow improvements #715

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .ci/testruns/default/templates/testrun.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,35 @@ spec:

testflow:
- name: infrastructure-test
definition:
name: infrastructure-test
config:
- name: REGION
type: env
value: europe-west1
- name: RECONCILER
type: env
value: tf
- name: infra-flow-test
definition:
name: infrastructure-test
config:
- name: REGION
type: env
value: europe-west1
- name: RECONCILER
type: env
value: flow
- name: infra-migrate-test
definition:
name: infrastructure-test
config:
- name: REGION
type: env
value: europe-west1
- name: RECONCILER
type: env
value: migrate
- name: bastion-test
definition:
name: bastion-test
Expand Down
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ issues:
# typecheck:
- "undeclared name: `.*`"
- "\".*\" imported but not used"
exclude-rules:
- linters:
- staticcheck
text: "SA1019:" # Excludes messages where deprecated variables are used
3 changes: 3 additions & 0 deletions .test-defs/infrastructure-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ spec:
owner: gardener-oq@listserv.sap.com
description: Integration test for infrastructure creation and deletion
activeDeadlineSeconds: 1800
behavior:
- serial

command: [bash, -c]
args:
Expand All @@ -14,5 +16,6 @@ spec:
--kubeconfig=$TM_KUBECONFIG_PATH/testmachinery.config
--service-account="${SERVICEACCOUNT_JSON}"
--region=$REGION
--reconciler=${RECONCILER}

image: golang:1.22.1
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ LD_FLAGS := "-w $(shell bash $(GARDENER_HACK_DIR)/get-build-l
LEADER_ELECTION := false
IGNORE_OPERATION_ANNOTATION := true
PLATFORM := linux/amd64
TEST_RECONCILER := tf

WEBHOOK_CONFIG_PORT := 8443
WEBHOOK_CONFIG_MODE := url
Expand Down Expand Up @@ -63,7 +64,8 @@ start:
--heartbeat-renew-interval-seconds=30 \
--webhook-config-service-port=443 \
--metrics-bind-address=:8080 \
--health-bind-address=:8081
--health-bind-address=:8081 \
--log-level=debug


.PHONY: start-admission
Expand Down Expand Up @@ -162,7 +164,8 @@ integration-test-infra:
--v -ginkgo.v -ginkgo.progress \
--kubeconfig=${KUBECONFIG} \
--service-account='$(shell cat $(SERVICE_ACCOUNT_FILE))' \
--region=$(REGION)
--region=$(REGION) \
--reconciler=$(TEST_RECONCILER)

.PHONY: integration-test-bastion
integration-test-bastion:
Expand Down
27 changes: 27 additions & 0 deletions hack/api-reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,33 @@ int32
</tr>
</tbody>
</table>
<h3 id="gcp.provider.extensions.gardener.cloud/v1alpha1.InfrastructureState">InfrastructureState
</h3>
<p>
<p>InfrastructureState contains state information of the infrastructure resource.</p>
</p>
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>data</code></br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Data is map to store things.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="gcp.provider.extensions.gardener.cloud/v1alpha1.InfrastructureStatus">InfrastructureStatus
</h3>
<p>
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/gcp/types_infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,13 @@ type FlowLogs struct {
// Metadata configures whether metadata fields should be added to the reported VPC flow logs.
Metadata *string
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// InfrastructureState contains state information of the infrastructure resource.
type InfrastructureState struct {
metav1.TypeMeta
// Data is map to store things.
// +optional
Data map[string]string
}
10 changes: 10 additions & 0 deletions pkg/apis/gcp/v1alpha1/types_infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,13 @@ type FlowLogs struct {
// +optional
Metadata *string `json:"metadata,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// InfrastructureState contains state information of the infrastructure resource.
type InfrastructureState struct {
metav1.TypeMeta `json:",inline"`
// Data is map to store things.
AndreasBurger marked this conversation as resolved.
Show resolved Hide resolved
// +optional
Data map[string]string `json:"data,omitempty"`
}
30 changes: 30 additions & 0 deletions pkg/apis/gcp/v1alpha1/zz_generated.conversion.go

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

32 changes: 32 additions & 0 deletions pkg/apis/gcp/v1alpha1/zz_generated.deepcopy.go

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

32 changes: 32 additions & 0 deletions pkg/apis/gcp/zz_generated.deepcopy.go

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

77 changes: 23 additions & 54 deletions pkg/controller/infrastructure/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@ package infrastructure

AndreasBurger marked this conversation as resolved.
Show resolved Hide resolved
import (
"context"
"strings"
"fmt"

extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
"github.com/gardener/gardener/extensions/pkg/controller/infrastructure"
"github.com/gardener/gardener/extensions/pkg/terraformer"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/v1alpha1"
"github.com/gardener/gardener-extension-provider-gcp/pkg/controller/infrastructure/infraflow"
"github.com/gardener/gardener-extension-provider-gcp/pkg/gcp"
"github.com/gardener/gardener-extension-provider-gcp/pkg/internal"
infrainternal "github.com/gardener/gardener-extension-provider-gcp/pkg/internal/infrastructure"
)

type actuator struct {
Expand All @@ -39,63 +34,37 @@ func NewActuator(mgr manager.Manager, disableProjectedTokenMount bool) infrastru
}
}

func (a *actuator) updateProviderStatus(
ctx context.Context,
infra *extensionsv1alpha1.Infrastructure,
status *v1alpha1.InfrastructureStatus,
state *runtime.RawExtension,
) error {
patch := client.MergeFrom(infra.DeepCopy())
infra.Status.ProviderStatus = &runtime.RawExtension{Object: status}
infra.Status.State = state
return a.client.Status().Patch(ctx, infra, patch)
}

func (a *actuator) cleanupTerraformerResources(ctx context.Context, log logr.Logger, infra *extensionsv1alpha1.Infrastructure) error {
tf, err := internal.NewTerraformer(log, a.restConfig, infrainternal.TerraformerPurpose, infra, a.disableProjectedTokenMount)
if err != nil {
// CleanupTerraformerResources deletes terraformer artifacts (config, state, secrets).
func CleanupTerraformerResources(ctx context.Context, tf terraformer.Terraformer) error {
if err := tf.EnsureCleanedUp(ctx); err != nil {
return err
}

if err := tf.CleanupConfiguration(ctx); err != nil {
return err
}

return tf.RemoveTerraformerFinalizerFromConfig(ctx) // Explicitly clean up the terraformer finalizers
return tf.RemoveTerraformerFinalizerFromConfig(ctx)
}

func getFlowStateFromInfrastructureStatus(infrastructure *extensionsv1alpha1.Infrastructure) (*infraflow.FlowState, error) {
if infrastructure.Status.State == nil || len(infrastructure.Status.State.Raw) == 0 {
return nil, nil
}

stateJSON, err := infrastructure.Status.State.MarshalJSON()
if err != nil {
return nil, err
}

isFlowState, err := infraflow.IsJSONFlowState(stateJSON)
if err != nil {
return nil, err
}
if isFlowState {
return infraflow.NewFlowStateFromJSON(stateJSON)
func patchProviderStatusAndState(
ctx context.Context,
runtimeClient client.Client,
infra *extensionsv1alpha1.Infrastructure,
status *v1alpha1.InfrastructureStatus,
state *runtime.RawExtension,
) error {
patch := client.MergeFrom(infra.DeepCopy())
if status != nil {
infra.Status.ProviderStatus = &runtime.RawExtension{Object: status}
}

return nil, nil
}

func shouldUseFlow(infra *extensionsv1alpha1.Infrastructure, cluster *extensionscontroller.Cluster) (bool, error) {
state, err := getFlowStateFromInfrastructureStatus(infra)
if err != nil {
return false, err
if state != nil {
infra.Status.State = state
}

if state != nil {
return true, nil
if data, err := patch.Data(infra); err != nil {
return fmt.Errorf("failed getting patch data for infra %s: %w", infra.Name, err)
} else if string(data) == `{}` {
return nil
}

return strings.EqualFold(infra.Annotations[gcp.AnnotationKeyUseFlow], "true") ||
(cluster.Shoot != nil && strings.EqualFold(cluster.Shoot.Annotations[gcp.AnnotationKeyUseFlow], "true")) ||
(cluster.Seed != nil && strings.EqualFold(cluster.Seed.Labels[gcp.SeedLabelKeyUseFlow], "true")), nil
return runtimeClient.Status().Patch(ctx, infra, patch)
}
Loading
Loading