Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 17 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/sprig/v3 v3.3.0
github.com/aws/aws-sdk-go v1.55.7
github.com/aws/aws-sdk-go v1.44.122
github.com/aws/aws-sdk-go-v2 v1.41.0
github.com/aws/aws-sdk-go-v2/config v1.32.6
github.com/aws/aws-sdk-go-v2/service/ec2 v1.279.0
github.com/aws/aws-sdk-go-v2/service/eks v1.76.3
github.com/aws/aws-sdk-go-v2/service/sts v1.41.5
github.com/aws/smithy-go v1.24.0
github.com/bbalet/stopwords v1.0.0
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d
github.com/enescakir/emoji v1.0.0
Expand Down Expand Up @@ -98,6 +104,16 @@ require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/ashanbrown/forbidigo v1.6.0 // indirect
github.com/ashanbrown/makezero v1.2.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.19.6 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.16 // indirect
github.com/aws/aws-sdk-go-v2/service/signin v1.0.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.30.8 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.12 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bkielbasa/cyclop v1.2.3 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
Expand Down
35 changes: 33 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,40 @@ github.com/ashanbrown/forbidigo v1.6.0 h1:D3aewfM37Yb3pxHujIPSpTf6oQk9sc9WZi8ger
github.com/ashanbrown/forbidigo v1.6.0/go.mod h1:Y8j9jy9ZYAEHXdu723cUlraTqbzjKF1MUyfOKL+AjcU=
github.com/ashanbrown/makezero v1.2.0 h1:/2Lp1bypdmK9wDIq7uWBlDF1iMUpIIS4A+pF6C9IEUU=
github.com/ashanbrown/makezero v1.2.0/go.mod h1:dxlPhHbDMC6N6xICzFBSK+4njQDdK8euNO0qjQMtGY4=
github.com/aws/aws-sdk-go v1.44.122 h1:p6mw01WBaNpbdP2xrisz5tIkcNwzj/HysobNoaAHjgo=
github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.55.7 h1:UJrkFq7es5CShfBwlWAC8DA077vp8PyVbQd3lqLiztE=
github.com/aws/aws-sdk-go v1.55.7/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go-v2 v1.41.0 h1:tNvqh1s+v0vFYdA1xq0aOJH+Y5cRyZ5upu6roPgPKd4=
github.com/aws/aws-sdk-go-v2 v1.41.0/go.mod h1:MayyLB8y+buD9hZqkCW3kX1AKq07Y5pXxtgB+rRFhz0=
github.com/aws/aws-sdk-go-v2/config v1.32.6 h1:hFLBGUKjmLAekvi1evLi5hVvFQtSo3GYwi+Bx4lpJf8=
github.com/aws/aws-sdk-go-v2/config v1.32.6/go.mod h1:lcUL/gcd8WyjCrMnxez5OXkO3/rwcNmvfno62tnXNcI=
github.com/aws/aws-sdk-go-v2/credentials v1.19.6 h1:F9vWao2TwjV2MyiyVS+duza0NIRtAslgLUM0vTA1ZaE=
github.com/aws/aws-sdk-go-v2/credentials v1.19.6/go.mod h1:SgHzKjEVsdQr6Opor0ihgWtkWdfRAIwxYzSJ8O85VHY=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.16 h1:80+uETIWS1BqjnN9uJ0dBUaETh+P1XwFy5vwHwK5r9k=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.16/go.mod h1:wOOsYuxYuB/7FlnVtzeBYRcjSRtQpAW0hCP7tIULMwo=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.16 h1:rgGwPzb82iBYSvHMHXc8h9mRoOUBZIGFgKb9qniaZZc=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.16/go.mod h1:L/UxsGeKpGoIj6DxfhOWHWQ/kGKcd4I1VncE4++IyKA=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.16 h1:1jtGzuV7c82xnqOVfx2F0xmJcOw5374L7N6juGW6x6U=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.16/go.mod h1:M2E5OQf+XLe+SZGmmpaI2yy+J326aFf6/+54PoxSANc=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 h1:WKuaxf++XKWlHWu9ECbMlha8WOEGm0OUEZqm4K/Gcfk=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4/go.mod h1:ZWy7j6v1vWGmPReu0iSGvRiise4YI5SkR3OHKTZ6Wuc=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.279.0 h1:o7eJKe6VYAnqERPlLAvDW5VKXV6eTKv1oxTpMoDP378=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.279.0/go.mod h1:Wg68QRgy2gEGGdmTPU/UbVpdv8sM14bUZmF64KFwAsY=
github.com/aws/aws-sdk-go-v2/service/eks v1.76.3 h1:840uwcJTIwrMPLuEUQVFKZbPgwnYzc5WDyXMiMYm5Ts=
github.com/aws/aws-sdk-go-v2/service/eks v1.76.3/go.mod h1:7IU8o/Snul26xioEWN5tgoOas1ISPGsiq5gME5rPh3o=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4 h1:0ryTNEdJbzUCEWkVXEXoqlXV72J5keC1GvILMOuD00E=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4/go.mod h1:HQ4qwNZh32C3CBeO6iJLQlgtMzqeG17ziAA/3KDJFow=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.16 h1:oHjJHeUy0ImIV0bsrX0X91GkV5nJAyv1l1CC9lnO0TI=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.16/go.mod h1:iRSNGgOYmiYwSCXxXaKb9HfOEj40+oTKn8pTxMlYkRM=
github.com/aws/aws-sdk-go-v2/service/signin v1.0.4 h1:HpI7aMmJ+mm1wkSHIA2t5EaFFv5EFYXePW30p1EIrbQ=
github.com/aws/aws-sdk-go-v2/service/signin v1.0.4/go.mod h1:C5RdGMYGlfM0gYq/tifqgn4EbyX99V15P2V3R+VHbQU=
github.com/aws/aws-sdk-go-v2/service/sso v1.30.8 h1:aM/Q24rIlS3bRAhTyFurowU8A0SMyGDtEOY/l/s/1Uw=
github.com/aws/aws-sdk-go-v2/service/sso v1.30.8/go.mod h1:+fWt2UHSb4kS7Pu8y+BMBvJF0EWx+4H0hzNwtDNRTrg=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.12 h1:AHDr0DaHIAo8c9t1emrzAlVDFp+iMMKnPdYy6XO4MCE=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.12/go.mod h1:GQ73XawFFiWxyWXMHWfhiomvP3tXtdNar/fi8z18sx0=
github.com/aws/aws-sdk-go-v2/service/sts v1.41.5 h1:SciGFVNZ4mHdm7gpD1dgZYnCuVdX1s+lFTg4+4DOy70=
github.com/aws/aws-sdk-go-v2/service/sts v1.41.5/go.mod h1:iW40X4QBmUxdP+fZNOpfmkdMZqsovezbAeO+Ubiv2pk=
github.com/aws/smithy-go v1.24.0 h1:LpilSUItNPFr1eY85RYgTIg5eIEPtvFbskaFcmmIUnk=
github.com/aws/smithy-go v1.24.0/go.mod h1:LEj2LM3rBRQJxPZTB4KuzZkaZYnZPnvgIhb4pu07mx0=
github.com/bbalet/stopwords v1.0.0 h1:0TnGycCtY0zZi4ltKoOGRFIlZHv0WqpoIGUsObjztfo=
github.com/bbalet/stopwords v1.0.0/go.mod h1:sAWrQoDMfqARGIn4s6dp7OW7ISrshUD8IP2q3KoqPjc=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
11 changes: 4 additions & 7 deletions pkg/cmd/preload/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,23 @@ import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go-v2/config"

load "github.com/appvia/terranetes-controller/pkg/utils/preload"
"github.com/appvia/terranetes-controller/pkg/utils/preload/eks"
)

// preload is responsible for retrieving the data from the cloud vendor
func (c *Command) preload(ctx context.Context) (load.Data, error) {
session, err := session.NewSession(&aws.Config{
Region: aws.String(c.Region),
})
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(c.Region))
if err != nil {
return nil, fmt.Errorf("failed to create aws session, error: %w", err)
return nil, fmt.Errorf("failed to create aws config, error: %w", err)
}

// @step: create the preloader for eks clusters
pe, err := eks.New(eks.Config{
ClusterName: c.Cluster,
Session: session,
Config: cfg,
})
if err != nil {
return nil, fmt.Errorf("failed to create preloader for cloud: %w", err)
Expand Down
45 changes: 24 additions & 21 deletions pkg/utils/preload/eks/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
package eks

import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/smithy-go"
)

// SanitizeName sanitizes the given name
Expand All @@ -34,34 +33,35 @@ func SanitizeName(name string) string {
}

// IsAWSErrorType returns true if the given error is an AWS error
// nolint:errorlint
func IsAWSErrorType(err error) bool {
if err == nil {
return false
}
_, ok := err.(awserr.Error)

return ok
var apiErr smithy.APIError
return errors.As(err, &apiErr)
}

// IsAWSError returns true if the given error is an AWS error
// nolint:errorlint
func IsAWSError(err error, code string) bool {
if err == nil || !IsAWSErrorType(err) {
return false
}

return err.(awserr.Error).Code() == code
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
return apiErr.ErrorCode() == code
}
return false
}

// ToMapTags converts a list of tags to a map
func ToMapTags(tags map[string]*string) []map[string]interface{} {
func ToMapTags(tags map[string]string) []map[string]interface{} {
var list []map[string]interface{}

for key, value := range tags {
list = append(list, map[string]interface{}{
"key": key,
"values": []string{aws.StringValue(value)},
"values": []string{value},
})
}

Expand All @@ -70,38 +70,41 @@ func ToMapTags(tags map[string]*string) []map[string]interface{} {

// IsResourceNotFoundException returns true if the given error is a resource not found error
func IsResourceNotFoundException(err error) bool {
return IsAWSError(err, eks.ErrCodeResourceNotFoundException)
return IsAWSError(err, "ResourceNotFoundException")
}

// HasTag returns true if the given tag is present in the given list of tags
func HasTag(tags []*ec2.Tag, key, value string) bool {
func HasTag(tags []types.Tag, key, value string) bool {
vm := regexp.MustCompile(fmt.Sprintf("^%s$", value))

tag, found := GetTag(tags, key)
if !found {
return false
}

return vm.MatchString(aws.StringValue(tag.Value))
return tag.Value != nil && vm.MatchString(*tag.Value)
}

// GetTagValue returns the value of the given tag key if it exist
func GetTagValue(tags []*ec2.Tag, key string) (string, bool) {
func GetTagValue(tags []types.Tag, key string) (string, bool) {
tag, found := GetTag(tags, key)
if !found {
return "", false
}

return aws.StringValue(tag.Value), true
if tag.Value == nil {
return "", false
}
return *tag.Value, true
}

// GetTag returns the value of the given tag key if it exist
func GetTag(tags []*ec2.Tag, key string) (*ec2.Tag, bool) {
func GetTag(tags []types.Tag, key string) (*types.Tag, bool) {
km := regexp.MustCompile(fmt.Sprintf("^%s$", key))

for _, tag := range tags {
if km.MatchString(aws.StringValue(tag.Key)) {
return tag, true
for i, tag := range tags {
if tag.Key != nil && km.MatchString(*tag.Key) {
return &tags[i], true
}
}

Expand Down
30 changes: 17 additions & 13 deletions pkg/utils/preload/eks/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,25 @@ import (
"errors"
"testing"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/smithy-go"
"github.com/stretchr/testify/assert"
"k8s.io/utils/pointer"
)

func TestIsAWSError(t *testing.T) {
assert.False(t, IsAWSError(errors.New(""), "code"))
assert.False(t, IsAWSError(awserr.New("test", "code", nil), "different_code"))
assert.False(t, IsAWSError(awserr.New("test", "code", nil), "code"))
apiErr1 := &smithy.GenericAPIError{Code: "test", Message: "code"}
assert.False(t, IsAWSError(apiErr1, "different_code"))
apiErr2 := &smithy.GenericAPIError{Code: "code", Message: "test"}
assert.True(t, IsAWSError(apiErr2, "code"))
}

func TestIsAWSErrorType(t *testing.T) {
assert.False(t, IsAWSErrorType(nil))
assert.False(t, IsAWSErrorType(errors.New("nope")))
assert.True(t, IsAWSErrorType(awserr.New("yes", "yes", nil)))
apiErr := &smithy.GenericAPIError{Code: "yes", Message: "yes"}
assert.True(t, IsAWSErrorType(apiErr))
}

func TestSantize(t *testing.T) {
Expand All @@ -53,20 +55,22 @@ func TestToMapTags(t *testing.T) {
"key": "foo",
"values": []string{"test"},
},
}, ToMapTags(map[string]*string{
"foo": pointer.String("test"),
}, ToMapTags(map[string]string{
"foo": "test",
}))
}

func TestIsResourceNotFoundException(t *testing.T) {
assert.False(t, IsResourceNotFoundException(nil))
assert.False(t, IsResourceNotFoundException(errors.New("nope")))
assert.False(t, IsResourceNotFoundException(awserr.New("nope", "nope", nil)))
assert.True(t, IsResourceNotFoundException(awserr.New(eks.ErrCodeResourceNotFoundException, "", nil)))
apiErr1 := &smithy.GenericAPIError{Code: "nope", Message: "nope"}
assert.False(t, IsResourceNotFoundException(apiErr1))
apiErr2 := &smithy.GenericAPIError{Code: "ResourceNotFoundException", Message: ""}
assert.True(t, IsResourceNotFoundException(apiErr2))
}

func TestHasTag(t *testing.T) {
tags := []*ec2.Tag{
tags := []types.Tag{
{
Key: pointer.String("Environment"),
Value: pointer.String("dev"),
Expand All @@ -86,7 +90,7 @@ func TestHasTag(t *testing.T) {
}

func TestGetTagValue(t *testing.T) {
tags := []*ec2.Tag{
tags := []types.Tag{
{
Key: pointer.String("Environment"),
Value: pointer.String("dev"),
Expand All @@ -110,7 +114,7 @@ func TestGetTagValue(t *testing.T) {
}

func TestGetTag(t *testing.T) {
tags := []*ec2.Tag{
tags := []types.Tag{
{
Key: pointer.String("Environment"),
Value: pointer.String("dev"),
Expand Down
43 changes: 43 additions & 0 deletions pkg/utils/preload/eks/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2023 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package eks

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go-v2/service/sts"
)

// EC2API defines the interface for EC2 operations
type EC2API interface {
DescribeSubnets(ctx context.Context, params *ec2.DescribeSubnetsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeSubnetsOutput, error)
DescribeRouteTables(ctx context.Context, params *ec2.DescribeRouteTablesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeRouteTablesOutput, error)
DescribeSecurityGroups(ctx context.Context, params *ec2.DescribeSecurityGroupsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeSecurityGroupsOutput, error)
}

// EKSAPI defines the interface for EKS operations
type EKSAPI interface {
DescribeCluster(ctx context.Context, params *eks.DescribeClusterInput, optFns ...func(*eks.Options)) (*eks.DescribeClusterOutput, error)
}

// STSAPI defines the interface for STS operations
type STSAPI interface {
GetCallerIdentity(ctx context.Context, params *sts.GetCallerIdentityInput, optFns ...func(*sts.Options)) (*sts.GetCallerIdentityOutput, error)
}
Loading