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

add ginkgo automation framework #1416

Merged
merged 1 commit into from
Apr 8, 2021
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
79 changes: 79 additions & 0 deletions test/framework/framework.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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 framework

import (
eniConfig "github.com/aws/amazon-vpc-cni-k8s/pkg/apis/crd/v1alpha1"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/aws"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s"

. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type Framework struct {
Options Options
K8sClient client.Client
CloudServices aws.Cloud
K8sResourceManagers k8s.ResourceManagers
}

func New(options Options) *Framework {
err := options.Validate()
Expect(err).ToNot(HaveOccurred())

config, err := clientcmd.BuildConfigFromFlags("", options.KubeConfig)
Expect(err).ToNot(HaveOccurred())

config.QPS = 20
config.Burst = 30

k8sSchema := runtime.NewScheme()
clientgoscheme.AddToScheme(k8sSchema)
eniConfig.AddToScheme(k8sSchema)

stopChan := ctrl.SetupSignalHandler()
cache, err := cache.New(config, cache.Options{Scheme: k8sSchema})
Expect(err).NotTo(HaveOccurred())

go func() {
cache.Start(stopChan)
}()
cache.WaitForCacheSync(stopChan)
realClient, err := client.New(config, client.Options{Scheme: k8sSchema})
Expect(err).NotTo(HaveOccurred())

k8sClient := client.DelegatingClient{
Reader: &client.DelegatingReader{
CacheReader: cache,
ClientReader: realClient,
},
Writer: realClient,
StatusClient: realClient,
}

cloudConfig := aws.CloudConfig{Region: options.AWSRegion, VpcID: options.AWSVPCID}

return &Framework{
Options: options,
K8sClient: k8sClient,
CloudServices: aws.NewCloud(cloudConfig),
K8sResourceManagers: k8s.NewResourceManager(k8sClient),
}
}
68 changes: 68 additions & 0 deletions test/framework/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 framework

import (
"flag"

"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd"
)

var GlobalOptions Options

func init() {
GlobalOptions.BindFlags()
}

type Options struct {
KubeConfig string
ClusterName string
AWSRegion string
AWSVPCID string
NgNameLabelKey string
NgNameLabelVal string
}

func (options *Options) BindFlags() {
flag.StringVar(&options.KubeConfig, "cluster-kubeconfig", "", "Path to kubeconfig containing embedded authinfo (required)")
flag.StringVar(&options.ClusterName, "cluster-name", "", `Kubernetes cluster name (required)`)
flag.StringVar(&options.AWSRegion, "aws-region", "", `AWS Region for the kubernetes cluster`)
flag.StringVar(&options.AWSVPCID, "aws-vpc-id", "", `AWS VPC ID for the kubernetes cluster`)
flag.StringVar(&options.NgNameLabelKey, "ng-name-label-key", "eks.amazonaws.com/nodegroup", "label key used to identify nodegroup name")
flag.StringVar(&options.NgNameLabelVal, "ng-name-label-val", "", "label value with the nodegroup name")
}

func (options *Options) Validate() error {
if len(options.KubeConfig) == 0 {
return errors.Errorf("%s must be set!", clientcmd.RecommendedConfigPathFlag)
}
if len(options.ClusterName) == 0 {
return errors.Errorf("%s must be set!", "cluster-name")
}
if len(options.AWSRegion) == 0 {
return errors.Errorf("%s must be set!", "aws-region")
}
if len(options.AWSVPCID) == 0 {
return errors.Errorf("%s must be set!", "aws-vpc-id")
}
if len(options.NgNameLabelKey) == 0 {
return errors.Errorf("%s must be set!", "ng-name-label-key")
}
if len(options.NgNameLabelVal) == 0 {
return errors.Errorf("%s must be set!", "ng-name-label-val")
}

return nil
}
56 changes: 56 additions & 0 deletions test/framework/resources/aws/cloud.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 aws

import (
"github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/aws/services"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
)

type CloudConfig struct {
VpcID string
Region string
}

type Cloud interface {
EC2() services.EC2
AutoScaling() services.AutoScaling
}

type defaultCloud struct {
cfg CloudConfig
ec2 services.EC2
autoScaling services.AutoScaling
}

func NewCloud(config CloudConfig) Cloud {
session := session.Must(session.NewSession(&aws.Config{
Region: aws.String(config.Region)}))

return &defaultCloud{
cfg: config,
ec2: services.NewEC2(session),
autoScaling: services.NewAutoScaling(session),
}
}

func (c *defaultCloud) EC2() services.EC2 {
return c.ec2
}

func (c *defaultCloud) AutoScaling() services.AutoScaling {
return c.autoScaling
}
52 changes: 52 additions & 0 deletions test/framework/resources/aws/services/autoscaling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
)

type AutoScaling interface {
DescribeAutoScalingGroup(autoScalingGroupName string) ([]*autoscaling.Group, error)
}

type defaultAutoScaling struct {
autoscalingiface.AutoScalingAPI
}

func NewAutoScaling(session *session.Session) AutoScaling {
return &defaultAutoScaling{
AutoScalingAPI: autoscaling.New(session),
}
}

func (d defaultAutoScaling) DescribeAutoScalingGroup(autoScalingGroupName string) ([]*autoscaling.Group, error) {
describeAutoScalingGroupIp := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: aws.StringSlice([]string{autoScalingGroupName}),
}
asg, err := d.AutoScalingAPI.DescribeAutoScalingGroups(describeAutoScalingGroupIp)
if err != nil {
return nil, err
}
if len(asg.AutoScalingGroups) == 0 {
return nil, fmt.Errorf("failed to find asg %s", autoScalingGroupName)
}

return asg.AutoScalingGroups, nil
}
51 changes: 51 additions & 0 deletions test/framework/resources/aws/services/ec2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)

type EC2 interface {
DescribeInstanceType(instanceType string) ([]*ec2.InstanceTypeInfo, error)
}

type defaultEC2 struct {
ec2iface.EC2API
}

func (d defaultEC2) DescribeInstanceType(instanceType string) ([]*ec2.InstanceTypeInfo, error) {
describeInstanceTypeIp := &ec2.DescribeInstanceTypesInput{
InstanceTypes: aws.StringSlice([]string{instanceType}),
}
describeInstanceOp, err := d.EC2API.DescribeInstanceTypes(describeInstanceTypeIp)
if err != nil {
return nil, err
}
if len(describeInstanceOp.InstanceTypes) == 0 {
return nil, fmt.Errorf("no instance type found in the output %s", instanceType)
}
return describeInstanceOp.InstanceTypes, nil
}

func NewEC2(session *session.Session) EC2 {
return &defaultEC2{
EC2API: ec2.New(session),
}
}
73 changes: 73 additions & 0 deletions test/framework/resources/k8s/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 k8s

import (
"github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s/resources"

"sigs.k8s.io/controller-runtime/pkg/client"
)

type ResourceManagers interface {
JobManager() resources.JobManager
DeploymentManager() resources.DeploymentManager
CustomResourceManager() resources.CustomResourceManager
NamespaceManager() resources.NamespaceManager
ServiceManager() resources.ServiceManager
NodeManager() resources.NodeManager
}

type defaultManager struct {
jobManager resources.JobManager
deploymentManager resources.DeploymentManager
customResourceManager resources.CustomResourceManager
namespaceManager resources.NamespaceManager
serviceManager resources.ServiceManager
nodeManager resources.NodeManager
}

func NewResourceManager(k8sClient client.DelegatingClient) ResourceManagers {
return &defaultManager{
jobManager: resources.NewDefaultJobManager(k8sClient),
deploymentManager: resources.NewDefaultDeploymentManager(k8sClient),
customResourceManager: resources.NewCustomResourceManager(k8sClient),
namespaceManager: resources.NewDefaultNamespaceManager(k8sClient),
serviceManager: resources.NewDefaultServiceManager(k8sClient),
nodeManager: resources.NewDefaultNodeManager(k8sClient),
}
}

func (m *defaultManager) JobManager() resources.JobManager {
return m.jobManager
}

func (m *defaultManager) DeploymentManager() resources.DeploymentManager {
return m.deploymentManager
}

func (m *defaultManager) CustomResourceManager() resources.CustomResourceManager {
return m.customResourceManager
}

func (m *defaultManager) NamespaceManager() resources.NamespaceManager {
return m.namespaceManager
}

func (m *defaultManager) ServiceManager() resources.ServiceManager {
return m.serviceManager
}

func (m *defaultManager) NodeManager() resources.NodeManager {
return m.nodeManager
}
Loading