Skip to content

Commit

Permalink
add test to verify cni-metrics-helper puts metrics to CW (#1461)
Browse files Browse the repository at this point in the history
* add test to verify cni-metrics-helper puts metrics to CW

* add helm chart for cni-metric-helper
  • Loading branch information
abhipth authored May 19, 2021
1 parent 324b060 commit d428990
Show file tree
Hide file tree
Showing 19 changed files with 974 additions and 83 deletions.
20 changes: 20 additions & 0 deletions test/framework/controller/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 controller

const (
CNIMetricsHelperChartDir = "/test/helm/charts/cni-metrics-helper"
CNIMetricsHelperReleaseName = "cni-metrics-helper"
CNIMetricHelperNamespace = "kube-system"
)
56 changes: 56 additions & 0 deletions test/framework/controller/installation_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 controller

import (
"os"
"path/filepath"
"strings"

"github.com/aws/amazon-vpc-cni-k8s/test/framework/helm"
)

type InstallationManager interface {
InstallCNIMetricsHelper(image string, tag string) error
UnInstallCNIMetricsHelper() error
}

func NewDefaultInstallationManager(manager helm.ReleaseManager) InstallationManager {
return &defaultInstallationManager{releaseManager: manager}
}

type defaultInstallationManager struct {
releaseManager helm.ReleaseManager
}

func (d *defaultInstallationManager) InstallCNIMetricsHelper(image string, tag string) error {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
projectRoot := strings.SplitAfter(dir, "amazon-vpc-cni-k8s")[0]

values := map[string]interface{}{
"image": map[string]interface{}{
"repository": image,
"tag": tag,
},
}

_, err := d.releaseManager.InstallUnPackagedRelease(projectRoot+CNIMetricsHelperChartDir,
CNIMetricsHelperReleaseName, CNIMetricHelperNamespace, values)
return err
}

func (d *defaultInstallationManager) UnInstallCNIMetricsHelper() error {
_, err := d.releaseManager.UninstallRelease(CNIMetricHelperNamespace, CNIMetricsHelperReleaseName)
return err
}
5 changes: 5 additions & 0 deletions test/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package framework

import (
eniConfig "github.com/aws/amazon-vpc-cni-k8s/pkg/apis/crd/v1alpha1"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/controller"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/helm"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/aws"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s"
sgp "github.com/aws/amazon-vpc-resource-controller-k8s/apis/vpcresources/v1beta1"
Expand All @@ -32,6 +34,7 @@ type Framework struct {
K8sClient client.Client
CloudServices aws.Cloud
K8sResourceManagers k8s.ResourceManagers
InstallationManager controller.InstallationManager
}

func New(options Options) *Framework {
Expand Down Expand Up @@ -77,5 +80,7 @@ func New(options Options) *Framework {
K8sClient: k8sClient,
CloudServices: aws.NewCloud(cloudConfig),
K8sResourceManagers: k8s.NewResourceManager(k8sClient, k8sSchema, config),
InstallationManager: controller.NewDefaultInstallationManager(
helm.NewDefaultReleaseManager(options.KubeConfig)),
}
}
80 changes: 80 additions & 0 deletions test/framework/helm/release_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 helm

import (
"fmt"

"github.com/prometheus/common/log"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/release"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

type ReleaseManager interface {
InstallUnPackagedRelease(chart string, releaseName string, namespace string,
values map[string]interface{}) (*release.Release, error)
UninstallRelease(namespace string, releaseName string) (*release.UninstallReleaseResponse, error)
}

type defaultReleaseManager struct {
kubeConfig string
}

func NewDefaultReleaseManager(kubeConfig string) ReleaseManager {
return &defaultReleaseManager{kubeConfig: kubeConfig}
}

func (d *defaultReleaseManager) InstallUnPackagedRelease(chart string, releaseName string, namespace string,
values map[string]interface{}) (*release.Release, error) {
actionConfig := d.obtainActionConfig(namespace)

installAction := action.NewInstall(actionConfig)
installAction.Namespace = namespace
installAction.Wait = true
installAction.ReleaseName = releaseName

cp, err := installAction.ChartPathOptions.LocateChart(chart, cli.New())
if err != nil {
return nil, err
}

chartRequested, err := loader.Load(cp)
if err != nil {
return nil, err
}

return installAction.Run(chartRequested, values)
}

func (d *defaultReleaseManager) UninstallRelease(namespace string, releaseName string) (*release.UninstallReleaseResponse, error) {
actionConfig := d.obtainActionConfig(namespace)

uninstallAction := action.NewUninstall(actionConfig)
return uninstallAction.Run(releaseName)
}

func (d *defaultReleaseManager) obtainActionConfig(namespace string) *action.Configuration {
cfgFlag := genericclioptions.NewConfigFlags(false)
cfgFlag.KubeConfig = &d.kubeConfig
cfgFlag.Namespace = &namespace
actionConfig := new(action.Configuration)
actionConfig.Init(cfgFlag, namespace, "secrets", func(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
log.Info(message)
})
return actionConfig
}
7 changes: 7 additions & 0 deletions test/framework/resources/aws/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Cloud interface {
IAM() services.IAM
AutoScaling() services.AutoScaling
CloudFormation() services.CloudFormation
CloudWatch() services.CloudWatch
}

type defaultCloud struct {
Expand All @@ -40,6 +41,7 @@ type defaultCloud struct {
iam services.IAM
autoScaling services.AutoScaling
cloudFormation services.CloudFormation
cloudWatch services.CloudWatch
}

func NewCloud(config CloudConfig) Cloud {
Expand All @@ -53,6 +55,7 @@ func NewCloud(config CloudConfig) Cloud {
eks: services.NewEKS(session, config.EKSEndpoint),
autoScaling: services.NewAutoScaling(session),
cloudFormation: services.NewCloudFormation(session),
cloudWatch: services.NewCloudWatch(session),
}
}

Expand All @@ -75,3 +78,7 @@ func (c *defaultCloud) EKS() services.EKS {
func (c *defaultCloud) IAM() services.IAM {
return c.iam
}

func (c *defaultCloud) CloudWatch() services.CloudWatch {
return c.cloudWatch
}
38 changes: 38 additions & 0 deletions test/framework/resources/aws/services/cloudwatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 services

import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
)

type CloudWatch interface {
GetMetricStatistics(getMetricStatisticsInput *cloudwatch.GetMetricStatisticsInput) (*cloudwatch.GetMetricStatisticsOutput, error)
}

type defaultCloudWatch struct {
cloudwatchiface.CloudWatchAPI
}

func NewCloudWatch(session *session.Session) CloudWatch {
return &defaultCloudWatch{
CloudWatchAPI: cloudwatch.New(session),
}
}

func (d *defaultCloudWatch) GetMetricStatistics(getMetricStatisticsInput *cloudwatch.GetMetricStatisticsInput) (*cloudwatch.GetMetricStatisticsOutput, error) {
return d.CloudWatchAPI.GetMetricStatistics(getMetricStatisticsInput)
}
37 changes: 37 additions & 0 deletions test/framework/resources/aws/services/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@ import (
"github.com/aws/aws-sdk-go/service/iam/iamiface"
)

type PolicyDocument struct {
Version string
Statement []StatementEntry
}

type StatementEntry struct {
Effect string
Action []string
Resource string
}

type IAM interface {
AttachRolePolicy(policyArn string, roleName string) error
DetachRolePolicy(policyARN string, roleName string) error
CreatePolicy(policyName string, policyDocument string) (*iam.CreatePolicyOutput, error)
DeletePolicy(policyARN string) error
GetInstanceProfile(instanceProfileName string) (*iam.GetInstanceProfileOutput, error)
}

type defaultIAM struct {
Expand All @@ -47,6 +61,29 @@ func (d *defaultIAM) DetachRolePolicy(policyARN string, roleName string) error {
return err
}

func (d *defaultIAM) CreatePolicy(policyName string, policyDocument string) (*iam.CreatePolicyOutput, error) {
createPolicyInput := &iam.CreatePolicyInput{
PolicyDocument: aws.String(policyDocument),
PolicyName: aws.String(policyName),
}
return d.IAMAPI.CreatePolicy(createPolicyInput)
}

func (d *defaultIAM) DeletePolicy(policyARN string) error {
deletePolicyInput := &iam.DeletePolicyInput{
PolicyArn: aws.String(policyARN),
}
_, err := d.IAMAPI.DeletePolicy(deletePolicyInput)
return err
}

func (d *defaultIAM) GetInstanceProfile(instanceProfileName string) (*iam.GetInstanceProfileOutput, error) {
getInstanceProfileInput := &iam.GetInstanceProfileInput{
InstanceProfileName: aws.String(instanceProfileName),
}
return d.IAMAPI.GetInstanceProfile(getInstanceProfileInput)
}

func NewIAM(session *session.Session) IAM {
return &defaultIAM{
IAMAPI: iam.New(session),
Expand Down
6 changes: 4 additions & 2 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ require (
github.com/aws/amazon-vpc-cni-k8s/test/agent v0.0.0-20210504235351-07ac754880d8
github.com/aws/amazon-vpc-resource-controller-k8s v1.0.7
github.com/aws/aws-sdk-go v1.37.23
github.com/google/gopacket v1.1.19 // indirect
github.com/gophercloud/gophercloud v0.1.0 // indirect
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.11.0
github.com/pkg/errors v0.9.1
github.com/prometheus/common v0.7.0
gopkg.in/yaml.v2 v2.4.0
helm.sh/helm/v3 v3.2.0
k8s.io/api v0.18.6
k8s.io/apimachinery v0.18.6
k8s.io/cli-runtime v0.18.0
k8s.io/client-go v0.18.6
rsc.io/letsencrypt v0.0.3 // indirect
sigs.k8s.io/controller-runtime v0.6.3
)
Loading

0 comments on commit d428990

Please sign in to comment.