Skip to content

Commit

Permalink
Merge pull request #18 from zzxwill/status-event
Browse files Browse the repository at this point in the history
Add more status.state for Configuration
  • Loading branch information
zzxwill committed May 13, 2021
2 parents 1d003e8 + 11e27bf commit 964b58f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
27 changes: 27 additions & 0 deletions api/types/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2019 The Crossplane Authors.
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 types

// A ResourceState represents the status of a resource
type ResourceState string

// Reasons a resource is or is not ready.
const (
Available ResourceState = "Available"
Unavailable ResourceState = "Unavailable"
Provisioning ResourceState = "Provisioning"
)
7 changes: 6 additions & 1 deletion api/v1beta1/configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
state "github.com/oam-dev/terraform-controller/api/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

Expand Down Expand Up @@ -47,7 +48,8 @@ type ConfigurationSpec struct {

// ConfigurationStatus defines the observed state of Configuration
type ConfigurationStatus struct {
State string `json:"state,omitempty"`
State state.ResourceState `json:"state,omitempty"`
Message string `json:"message,omitempty"`
Outputs map[string]Property `json:"outputs,omitempty"`
}

Expand All @@ -67,6 +69,9 @@ type Backend struct {
// +kubebuilder:object:root=true

// Configuration is the Schema for the configurations API
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="STATE",type="string",JSONPath=".status.state"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
type Configuration struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
12 changes: 12 additions & 0 deletions chart/crds/terraform.core.oam.dev_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ metadata:
creationTimestamp: null
name: configurations.terraform.core.oam.dev
spec:
additionalPrinterColumns:
- JSONPath: .status.state
name: STATE
type: string
- JSONPath: .metadata.creationTimestamp
name: AGE
type: date
group: terraform.core.oam.dev
names:
kind: Configuration
listKind: ConfigurationList
plural: configurations
singular: configuration
scope: Namespaced
subresources:
status: {}
validation:
openAPIV3Schema:
description: Configuration is the Schema for the configurations API
Expand Down Expand Up @@ -79,6 +88,8 @@ spec:
status:
description: ConfigurationStatus defines the observed state of Configuration
properties:
message:
type: string
outputs:
additionalProperties:
properties:
Expand All @@ -89,6 +100,7 @@ spec:
type: object
type: object
state:
description: A ResourceState represents the status of a resource
type: string
type: object
type: object
Expand Down
39 changes: 30 additions & 9 deletions controllers/configuration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
state "github.com/oam-dev/terraform-controller/api/types"

"github.com/go-logr/logr"
"github.com/pkg/errors"
Expand Down Expand Up @@ -84,7 +85,7 @@ func (r *ConfigurationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
configurationName = req.Name
tfInputConfigMapsName = fmt.Sprintf(TFInputConfigMapSName, configurationName)
)
klog.InfoS("reconciling Terraform Template...", "NamespacedName", req.NamespacedName)
klog.InfoS("reconciling Terraform Configuration...", "NamespacedName", req.NamespacedName)

// Terraform destroy
if err := r.Get(ctx, req.NamespacedName, &configuration); err != nil {
Expand All @@ -95,38 +96,50 @@ func (r *ConfigurationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
err := terraformDestroy(ctx, r.Client, ns, req.Name, destroyJobName, tfInputConfigMapsName)
return ctrl.Result{}, errors.Wrap(err, "continue reconciling to destroy cloud resource")
}
return ctrl.Result{}, errors.Wrap(err, "failed to call GET resource API")
}

// Terraform apply (create or update)
klog.InfoS("performing Terraform Apply (cloud resource create/update)", "Namespace", req.Namespace, "Name", req.Name)
applyJobName := configurationName + "-" + string(TerraformApply)
klog.InfoS("creating job", "Namespace", req.Namespace, "Name", applyJobName)
if err := terraformApply(ctx, r.Client, ns, configuration, applyJobName, tfInputConfigMapsName); err != nil {
return ctrl.Result{}, err
return ctrl.Result{RequeueAfter: 3}, errors.Wrap(err, "failed to create/update cloud resource")
}
return ctrl.Result{}, nil
return ctrl.Result{RequeueAfter: 10}, nil
}

func terraformApply(ctx context.Context, k8sClient client.Client, namespace string, configuration v1beta1.Configuration, applyJobName, tfInputConfigMapName string) error {
var gotJob batchv1.Job
klog.InfoS("terraform apply job", "Namespace", namespace, "Name", applyJobName)

var gotJob batchv1.Job
err := k8sClient.Get(ctx, client.ObjectKey{Name: applyJobName, Namespace: namespace}, &gotJob)
if err == nil {
if gotJob.Status.Succeeded == *pointer.Int32Ptr(1) {
outputs, err := getTFOutputs(ctx, k8sClient, configuration)
if err != nil {
return err
}
configuration.Status.State = "provisioned"
configuration.Status.Outputs = outputs
if err := k8sClient.Update(ctx, &configuration); err != nil {
return err
if configuration.Status.State != state.Available {
configuration.Status.State = state.Available
configuration.Status.Message = "Cloud resources are deployed and ready to use."
configuration.Status.Outputs = outputs
if err := k8sClient.Status().Update(ctx, &configuration); err != nil {
return err
}
}
}
return nil
}

if kerrors.IsNotFound(err) {
if configuration.Status.State != state.Provisioning {
configuration.Status.State = state.Provisioning
configuration.Status.Message = "Cloud resources are being provisioned."
if err := k8sClient.Status().Update(ctx, &configuration); err != nil {
return err
}
}

configurationType, inputConfiguration, err := util.ValidConfiguration(&configuration)
if err != nil {
return err
Expand All @@ -142,6 +155,14 @@ func terraformApply(ctx context.Context, k8sClient client.Client, namespace stri
}
return nil
}

if configuration.Status.State != state.Unavailable {
configuration.Status.State = state.Unavailable
configuration.Status.Message = fmt.Sprintf("The state of cloud resources is unavailable due to %s", err)
if err := k8sClient.Status().Update(ctx, &configuration); err != nil {
return err
}
}
return err
}

Expand Down
2 changes: 1 addition & 1 deletion examples/alibaba/configuration_hcl_oss.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: terraform.core.oam.dev/v1beta1
kind: Configuration
metadata:
name: alibaba-oss
name: alibaba-oss-bucket-hcl
spec:
hcl: |
resource "alicloud_oss_bucket" "bucket-acl" {
Expand Down
2 changes: 1 addition & 1 deletion examples/alibaba/configuration_hcl_rds.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: terraform.core.oam.dev/v1beta1
kind: Configuration
metadata:
name: alibaba-rds
name: alibaba-rds-mysql-hcl
spec:
hcl: |
module "rds" {
Expand Down
2 changes: 1 addition & 1 deletion examples/alibaba/configuration_json_oss.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: terraform.core.oam.dev/v1beta1
kind: Configuration
metadata:
name: alibaba-oss
name: alibaba-oss-bucket-json
spec:
JSON: |
{
Expand Down

0 comments on commit 964b58f

Please sign in to comment.