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

Commit

Permalink
Merge pull request #104 from christopherhein/feature/85-log-to-k8s-ev…
Browse files Browse the repository at this point in the history
…ents

Adding Kubernetes Recorder to log to the standard events  …
  • Loading branch information
Christopher Hein committed Oct 4, 2018
2 parents f0170cf + 9fe20ce commit a4318ff
Show file tree
Hide file tree
Showing 7,391 changed files with 259,458 additions and 144,450 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
270 changes: 234 additions & 36 deletions Gopkg.lock

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ required = ["k8s.io/code-generator/cmd/client-gen"]

[[constraint]]
name = "k8s.io/api"
version = "kubernetes-1.10.4"
version = "kubernetes-1.11.3"

[[constraint]]
name = "k8s.io/apiextensions-apiserver"
version = "kubernetes-1.10.4"
version = "kubernetes-1.11.3"

[[constraint]]
name = "k8s.io/apimachinery"
version = "kubernetes-1.10.4"
version = "kubernetes-1.11.3"

[[constraint]]
name = "k8s.io/client-go"
version = "7.0.0"
version = "8.0.0"

[[constraint]]
name = "k8s.io/code-generator"
version = "kubernetes-1.10.4"
version = "kubernetes-1.11.3"

[[override]]
name = "github.com/json-iterator/go"
revision = "f2b4162afba35581b6d4a50d3b8f34e33c144682"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ aws-codegen:

.PHONY: k8s-codegen
k8s-codegen:
./codegen.sh
./hack/update-codegen.sh

.PHONY: codegen
codegen: aws-codegen k8s-codegen
Expand Down
3 changes: 2 additions & 1 deletion cmd/aws-service-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var (
rootCmd = &cobra.Command{
Use: "aws-operator",
Short: "AWS Operator manages your AWS Infrastructure using CRDs and Operators",
Long: `TODO WRITE THIS`,
Long: `AWS Operator manages your AWS Infrastructure using CRDs and Operators.
With a single manifest file you can now model both the application and the resource necessary to run it.`,
Run: func(c *cobra.Command, _ []string) {
c.Help()
},
Expand Down
43 changes: 27 additions & 16 deletions code-generation/pkg/codegen/assets/controller.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (

"github.com/awslabs/aws-service-operator/pkg/config"
{{- if .Spec.Queue}}
"github.com/awslabs/aws-service-operator/pkg/queue"
"github.com/awslabs/aws-service-operator/pkg/queue"
corev1 "k8s.io/api/core/v1"
"github.com/iancoleman/strcase"
"strings"
{{- end}}
opkit "github.com/christopherhein/operator-kit"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
Expand Down Expand Up @@ -50,7 +53,7 @@ type Controller struct {
config *config.Config
context *opkit.Context
awsclientset awsclient.ServiceoperatorV1alpha1Interface
topicARN string
topicARN string
}

// NewController create controller for watching object store custom resources created
Expand Down Expand Up @@ -106,27 +109,35 @@ func QueueUpdater(config *config.Config, msg *queue.MessageBody) error {
}

if name != "" && namespace != "" {
annotations := map[string]string{
"StackID": msg.ParsedMessage["StackId"],
"StackName": msg.ParsedMessage["StackName"],
"ResourceType": msg.ParsedMessage["ResourceType"],
}
if msg.ParsedMessage["ResourceStatus"] == "ROLLBACK_COMPLETE" {
err := deleteStack(config, name, namespace, msg.ParsedMessage["StackId"])
obj, err := deleteStack(config, name, namespace, msg.ParsedMessage["StackId"])
if err != nil {
return err
}
config.Recorder.AnnotatedEventf(obj, annotations, corev1.EventTypeWarning, strcase.ToCamel(strings.ToLower(msg.ParsedMessage["ResourceStatus"])), msg.ParsedMessage["ResourceStatusReason"])
} else if msg.ParsedMessage["ResourceStatus"] == "DELETE_COMPLETE" {
err := updateStatus(config, name, namespace, msg.ParsedMessage["StackId"], msg.ParsedMessage["ResourceStatus"], msg.ParsedMessage["ResourceStatusReason"])
obj, err := updateStatus(config, name, namespace, msg.ParsedMessage["StackId"], msg.ParsedMessage["ResourceStatus"], msg.ParsedMessage["ResourceStatusReason"])
if err != nil {
return err
}

config.Recorder.AnnotatedEventf(obj, annotations, corev1.EventTypeWarning, strcase.ToCamel(strings.ToLower(msg.ParsedMessage["ResourceStatus"])), msg.ParsedMessage["ResourceStatusReason"])
err = incrementRollbackCount(config, name, namespace)
if err != nil {
return err
}
} else {
err := updateStatus(config, name, namespace, msg.ParsedMessage["StackId"], msg.ParsedMessage["ResourceStatus"], msg.ParsedMessage["ResourceStatusReason"])
obj, err := updateStatus(config, name, namespace, msg.ParsedMessage["StackId"], msg.ParsedMessage["ResourceStatus"], msg.ParsedMessage["ResourceStatusReason"])
if err != nil {
return err
}
config.Recorder.AnnotatedEventf(obj, annotations, corev1.EventTypeNormal, strcase.ToCamel(strings.ToLower(msg.ParsedMessage["ResourceStatus"])), msg.ParsedMessage["ResourceStatusReason"])
}

}

return nil
Expand All @@ -147,7 +158,7 @@ func (c *Controller) onAdd(obj interface{}) {
c.config.Logger.Infof("added {{.Spec.Resource.Name}} '%s' with stackID '%s'", s.Name, string(*output.StackId))
c.config.Logger.Infof("view at https://console.aws.amazon.com/cloudformation/home?#/stack/detail?stackId=%s", string(*output.StackId))

err = updateStatus(c.config, s.Name, s.Namespace, string(*output.StackId), "CREATE_IN_PROGRESS", "")
_, err = updateStatus(c.config, s.Name, s.Namespace, string(*output.StackId), "CREATE_IN_PROGRESS", "")
if err != nil {
c.config.Logger.WithError(err).Error("error updating status")
}
Expand Down Expand Up @@ -176,7 +187,7 @@ func (c *Controller) onUpdate(oldObj, newObj interface{}) {
c.config.Logger.Infof("updated {{.Spec.Resource.Name}} '%s' with params '%s'", no.Name, string(*output.StackId))
c.config.Logger.Infof("view at https://console.aws.amazon.com/cloudformation/home?#/stack/detail?stackId=%s", string(*output.StackId))

err = updateStatus(c.config, oo.Name, oo.Namespace, string(*output.StackId), "UPDATE_IN_PROGRESS", "")
_, err = updateStatus(c.config, oo.Name, oo.Namespace, string(*output.StackId), "UPDATE_IN_PROGRESS", "")
if err != nil {
c.config.Logger.WithError(err).Error("error updating status")
}
Expand Down Expand Up @@ -225,13 +236,13 @@ func incrementRollbackCount(config *config.Config, name string, namespace string
return nil
}

func updateStatus(config *config.Config, name string, namespace string, stackID string, status string, reason string) error {
func updateStatus(config *config.Config, name string, namespace string, stackID string, status string, reason string) (*awsV1alpha1.{{.Spec.Kind}}, error) {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.{{.Spec.PluralName}}(namespace).Get(name, metav1.GetOptions{})
if err != nil {
logger.WithError(err).Error("error getting {{.Spec.Resource.Plural}}")
return err
return nil, err
}

resourceCopy := resource.DeepCopy()
Expand Down Expand Up @@ -259,7 +270,7 @@ func updateStatus(config *config.Config, name string, namespace string, stackID
_, err = clientSet.{{.Spec.PluralName}}(namespace).Update(resourceCopy)
if err != nil {
logger.WithError(err).Error("error updating resource")
return err
return nil, err
}

if helpers.IsStackComplete(status, false) {
Expand All @@ -268,26 +279,26 @@ func updateStatus(config *config.Config, name string, namespace string, stackID
logger.WithError(err).Info("error syncing resources")
}
}
return nil
return resourceCopy, nil
}

func deleteStack(config *config.Config, name string, namespace string, stackID string) error {
func deleteStack(config *config.Config, name string, namespace string, stackID string) (*awsV1alpha1.{{.Spec.Kind}}, error) {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.{{.Spec.PluralName}}(namespace).Get(name, metav1.GetOptions{})
if err != nil {
logger.WithError(err).Error("error getting {{.Spec.Resource.Plural}}")
return err
return nil, err
}

cft := New(config, resource, "")
err = cft.DeleteStack()
if err != nil {
return err
return nil, err
}

err = cft.WaitUntilStackDeleted()
return err
return resource, err
}

func syncAdditionalResources(config *config.Config, s *awsV1alpha1.{{.Spec.Kind}}) (err error) {
Expand Down
24 changes: 12 additions & 12 deletions code-generation/pkg/codegen/templates.go

Large diffs are not rendered by default.

21 changes: 0 additions & 21 deletions codegen.sh

This file was deleted.

13 changes: 13 additions & 0 deletions hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
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.
*/
17 changes: 17 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname ${BASH_SOURCE})/..
CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${SCRIPT_ROOT}; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}

# generate the code with:
# --output-base because this script should also be able to run inside the vendor dir of
# k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
# instead of the $GOPATH directly. For normal projects this can be dropped.
${CODEGEN_PKG}/generate-groups.sh all \
github.com/awslabs/aws-service-operator/pkg/client \
github.com/awslabs/aws-service-operator/pkg/apis \
"service-operator.aws:v1alpha1" \
--output-base "$(dirname ${BASH_SOURCE})/../../.." \
--go-header-file ${SCRIPT_ROOT}/hack/boilerplate.go.txt
2 changes: 2 additions & 0 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
opkit "github.com/christopherhein/operator-kit"
"github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
)

// Config defines the configuration for the operator
Expand All @@ -22,6 +23,7 @@ type Config struct {
ClusterName string
Bucket string
AccountID string
Recorder record.EventRecorder
}

// LoggingConfig defines the attributes for the logger
Expand Down
39 changes: 25 additions & 14 deletions pkg/operator/dynamodb/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import (
"github.com/awslabs/aws-service-operator/pkg/config"
"github.com/awslabs/aws-service-operator/pkg/queue"
opkit "github.com/christopherhein/operator-kit"
"github.com/iancoleman/strcase"
corev1 "k8s.io/api/core/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/client-go/tools/cache"
"strings"

awsapi "github.com/awslabs/aws-service-operator/pkg/apis/service-operator.aws"
awsV1alpha1 "github.com/awslabs/aws-service-operator/pkg/apis/service-operator.aws/v1alpha1"
Expand Down Expand Up @@ -96,27 +99,35 @@ func QueueUpdater(config *config.Config, msg *queue.MessageBody) error {
}

if name != "" && namespace != "" {
annotations := map[string]string{
"StackID": msg.ParsedMessage["StackId"],
"StackName": msg.ParsedMessage["StackName"],
"ResourceType": msg.ParsedMessage["ResourceType"],
}
if msg.ParsedMessage["ResourceStatus"] == "ROLLBACK_COMPLETE" {
err := deleteStack(config, name, namespace, msg.ParsedMessage["StackId"])
obj, err := deleteStack(config, name, namespace, msg.ParsedMessage["StackId"])
if err != nil {
return err
}
config.Recorder.AnnotatedEventf(obj, annotations, corev1.EventTypeWarning, strcase.ToCamel(strings.ToLower(msg.ParsedMessage["ResourceStatus"])), msg.ParsedMessage["ResourceStatusReason"])
} else if msg.ParsedMessage["ResourceStatus"] == "DELETE_COMPLETE" {
err := updateStatus(config, name, namespace, msg.ParsedMessage["StackId"], msg.ParsedMessage["ResourceStatus"], msg.ParsedMessage["ResourceStatusReason"])
obj, err := updateStatus(config, name, namespace, msg.ParsedMessage["StackId"], msg.ParsedMessage["ResourceStatus"], msg.ParsedMessage["ResourceStatusReason"])
if err != nil {
return err
}

config.Recorder.AnnotatedEventf(obj, annotations, corev1.EventTypeWarning, strcase.ToCamel(strings.ToLower(msg.ParsedMessage["ResourceStatus"])), msg.ParsedMessage["ResourceStatusReason"])
err = incrementRollbackCount(config, name, namespace)
if err != nil {
return err
}
} else {
err := updateStatus(config, name, namespace, msg.ParsedMessage["StackId"], msg.ParsedMessage["ResourceStatus"], msg.ParsedMessage["ResourceStatusReason"])
obj, err := updateStatus(config, name, namespace, msg.ParsedMessage["StackId"], msg.ParsedMessage["ResourceStatus"], msg.ParsedMessage["ResourceStatusReason"])
if err != nil {
return err
}
config.Recorder.AnnotatedEventf(obj, annotations, corev1.EventTypeNormal, strcase.ToCamel(strings.ToLower(msg.ParsedMessage["ResourceStatus"])), msg.ParsedMessage["ResourceStatusReason"])
}

}

return nil
Expand All @@ -134,7 +145,7 @@ func (c *Controller) onAdd(obj interface{}) {
c.config.Logger.Infof("added dynamodb '%s' with stackID '%s'", s.Name, string(*output.StackId))
c.config.Logger.Infof("view at https://console.aws.amazon.com/cloudformation/home?#/stack/detail?stackId=%s", string(*output.StackId))

err = updateStatus(c.config, s.Name, s.Namespace, string(*output.StackId), "CREATE_IN_PROGRESS", "")
_, err = updateStatus(c.config, s.Name, s.Namespace, string(*output.StackId), "CREATE_IN_PROGRESS", "")
if err != nil {
c.config.Logger.WithError(err).Error("error updating status")
}
Expand All @@ -158,7 +169,7 @@ func (c *Controller) onUpdate(oldObj, newObj interface{}) {
c.config.Logger.Infof("updated dynamodb '%s' with params '%s'", no.Name, string(*output.StackId))
c.config.Logger.Infof("view at https://console.aws.amazon.com/cloudformation/home?#/stack/detail?stackId=%s", string(*output.StackId))

err = updateStatus(c.config, oo.Name, oo.Namespace, string(*output.StackId), "UPDATE_IN_PROGRESS", "")
_, err = updateStatus(c.config, oo.Name, oo.Namespace, string(*output.StackId), "UPDATE_IN_PROGRESS", "")
if err != nil {
c.config.Logger.WithError(err).Error("error updating status")
}
Expand Down Expand Up @@ -196,13 +207,13 @@ func incrementRollbackCount(config *config.Config, name string, namespace string
return nil
}

func updateStatus(config *config.Config, name string, namespace string, stackID string, status string, reason string) error {
func updateStatus(config *config.Config, name string, namespace string, stackID string, status string, reason string) (*awsV1alpha1.DynamoDB, error) {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.DynamoDBs(namespace).Get(name, metav1.GetOptions{})
if err != nil {
logger.WithError(err).Error("error getting dynamodbs")
return err
return nil, err
}

resourceCopy := resource.DeepCopy()
Expand All @@ -223,7 +234,7 @@ func updateStatus(config *config.Config, name string, namespace string, stackID
_, err = clientSet.DynamoDBs(namespace).Update(resourceCopy)
if err != nil {
logger.WithError(err).Error("error updating resource")
return err
return nil, err
}

if helpers.IsStackComplete(status, false) {
Expand All @@ -232,26 +243,26 @@ func updateStatus(config *config.Config, name string, namespace string, stackID
logger.WithError(err).Info("error syncing resources")
}
}
return nil
return resourceCopy, nil
}

func deleteStack(config *config.Config, name string, namespace string, stackID string) error {
func deleteStack(config *config.Config, name string, namespace string, stackID string) (*awsV1alpha1.DynamoDB, error) {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.DynamoDBs(namespace).Get(name, metav1.GetOptions{})
if err != nil {
logger.WithError(err).Error("error getting dynamodbs")
return err
return nil, err
}

cft := New(config, resource, "")
err = cft.DeleteStack()
if err != nil {
return err
return nil, err
}

err = cft.WaitUntilStackDeleted()
return err
return resource, err
}

func syncAdditionalResources(config *config.Config, s *awsV1alpha1.DynamoDB) (err error) {
Expand Down
Loading

0 comments on commit a4318ff

Please sign in to comment.