Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
Develop (#1)
Browse files Browse the repository at this point in the history
* refactor: Move functions and constants in resource utils

* chore: Update gitignore to ignore coverage report

* test: Add test for resources utils

* fix: Fix dependency lock file

* test: Add tests for version

* feat: Test if configuration is valid before using it

* refactor: Rename function in resource

* fix: Errors not constants

* fix: variable already exists
  • Loading branch information
oxyno-zeta authored Mar 4, 2019
1 parent 7b44409 commit 798a786
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,6 @@ ASALocalRun/
bin/
vendor/
_dist/
c.out
coverage.html

63 changes: 62 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@
[[constraint]]
branch = "master"
name = "github.com/dimiro1/health"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.1.4"
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ docker-publish-latest:
docker push oxynozeta/kubernetes-tagger:latest

test: dep ## Run unittests
$(GO) test -short ${PKG_LIST}
$(GO) test -short -cover -coverprofile=c.out ${PKG_LIST}

race: dep ## Run data race detector
$(GO) test -race -short ${PKG_LIST}
.PHONY: coverage-report
coverage-report:
$(GO) tool cover -html=c.out -o coverage.html

.PHONY: clean
clean:
Expand Down
6 changes: 6 additions & 0 deletions pkg/kubernetes-tagger/business/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func (context *Context) runForPV(pv *v1.PersistentVolume) error {
return nil
}

// Check if configuration is valid before continue
err = resource.CheckIfConfigurationValid()
if err != nil {
return err
}

// Get actual tags
actualTags, err := resource.GetActualTags()
if err != nil {
Expand Down
41 changes: 19 additions & 22 deletions pkg/kubernetes-tagger/resources/awsvolume.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resources

import (
"errors"
"fmt"
"net/url"
"strings"
Expand All @@ -12,11 +13,15 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/oxyno-zeta/kubernetes-tagger/pkg/kubernetes-tagger/config"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// ErrEmptyAWSConfiguration Error Empty AWS Configuration
var ErrEmptyAWSConfiguration = errors.New("AWS configuration is empty")

// ErrEmptyAWSRegionConfiguration Error Empty AWS Region Configuration
var ErrEmptyAWSRegionConfiguration = errors.New("AWS Region is empty in configuration")

// AWSVolume AWS Volume
type AWSVolume struct {
resourceType string
Expand All @@ -28,12 +33,6 @@ type AWSVolume struct {
log *logrus.Entry
}

// AWSVolumeResourceType AWS Volume Resource Type
const AWSVolumeResourceType = "volume"

// AWSResourcePlatform AWS Resource Platform
const AWSResourcePlatform = "aws"

// Type Get type
func (av *AWSVolume) Type() string {
return av.resourceType
Expand Down Expand Up @@ -84,9 +83,20 @@ func isAWSVolumeResource(pv *v1.PersistentVolume) bool {
return pv.Spec.AWSElasticBlockStore != nil
}

// CheckIfConfigurationValid Check if configuration is valid
func (av *AWSVolume) CheckIfConfigurationValid() error {
if av.awsConfig == nil {
return ErrEmptyAWSConfiguration
}
if av.awsConfig.Region == "" {
return ErrEmptyAWSRegionConfiguration
}
return nil
}

// GetAvailableTagValues Get available tags
func (av *AWSVolume) GetAvailableTagValues() (map[string]interface{}, error) {
pvc, err := av.getPersistentVolumeClaim()
pvc, err := getPersistentVolumeClaim(av.persistentVolume, av.k8sClient)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -222,16 +232,3 @@ func (av *AWSVolume) getAWSEC2Client() (*ec2.EC2, error) {
svc := ec2.New(sess)
return svc, nil
}

func (av *AWSVolume) getPersistentVolumeClaim() (*v1.PersistentVolumeClaim, error) {
claimRef := av.persistentVolume.Spec.ClaimRef
if claimRef == nil {
return nil, nil
}

pvc, err := av.k8sClient.CoreV1().PersistentVolumeClaims(claimRef.Namespace).Get(claimRef.Name, metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return nil, err
}
return pvc, nil
}
1 change: 1 addition & 0 deletions pkg/kubernetes-tagger/resources/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Resource interface {
Type() string
Platform() string
CanBeProcessed() bool
CheckIfConfigurationValid() error
GetAvailableTagValues() (map[string]interface{}, error)
GetActualTags() ([]*Tag, error)
ManageTags(delta *TagDelta) error
Expand Down
27 changes: 27 additions & 0 deletions pkg/kubernetes-tagger/resources/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package resources

import (
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// AWSVolumeResourceType AWS Volume Resource Type
const AWSVolumeResourceType = "volume"

// AWSResourcePlatform AWS Resource Platform
const AWSResourcePlatform = "aws"

func getPersistentVolumeClaim(persistentVolume *v1.PersistentVolume, k8sClient kubernetes.Interface) (*v1.PersistentVolumeClaim, error) {
claimRef := persistentVolume.Spec.ClaimRef
if claimRef == nil {
return nil, nil
}

pvc, err := k8sClient.CoreV1().PersistentVolumeClaims(claimRef.Namespace).Get(claimRef.Name, metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return nil, err
}
return pvc, nil
}
56 changes: 56 additions & 0 deletions pkg/kubernetes-tagger/resources/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package resources

import (
"testing"

"github.com/stretchr/testify/assert"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
testclient "k8s.io/client-go/kubernetes/fake"
)

func TestGetPersistentVolumeClaimWithoutClaimRef(t *testing.T) {
spec := v1.PersistentVolumeSpec{ClaimRef: nil}
pv := v1.PersistentVolume{Spec: spec}

// Call code
res, err := getPersistentVolumeClaim(&pv, nil)

assert.Nil(t, res)
assert.Nil(t, err)
}

func TestGetPersistentVolumeClaimWithClaimNotFound(t *testing.T) {
claimRef := v1.ObjectReference{Namespace: "test-claim-ref-namespace", Name: "test-claim-ref-name"}
spec := v1.PersistentVolumeSpec{ClaimRef: &claimRef}
pv := v1.PersistentVolume{Spec: spec}

client := testclient.NewSimpleClientset()

// Call code
res, err := getPersistentVolumeClaim(&pv, client)

assert.Nil(t, res)
assert.Nil(t, err)
}

func TestGetPersistentVolumeClaimWithClaimFound(t *testing.T) {
claimRefNamespace := "test-claim-ref-namespace"
claimRefName := "test-claim-ref-name"
claimRef := v1.ObjectReference{Namespace: claimRefNamespace, Name: claimRefName}
spec := v1.PersistentVolumeSpec{ClaimRef: &claimRef}
pv := v1.PersistentVolume{Spec: spec}

// PVC
pvc := &v1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{Name: claimRefName, Namespace: claimRefNamespace}}

client := testclient.NewSimpleClientset(pvc)

// Call code
res, err := getPersistentVolumeClaim(&pv, client)

assert.NotNil(t, res)
assert.Nil(t, err)
assert.Equal(t, pvc, res)
}
Loading

0 comments on commit 798a786

Please sign in to comment.