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

Compass runtime agent config apply #4984

Merged
merged 3 commits into from
Jul 24, 2019
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
2 changes: 2 additions & 0 deletions components/compass-runtime-agent/Gopkg.lock

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

11 changes: 11 additions & 0 deletions components/compass-runtime-agent/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ required = [
name = "github.com/kyma-project/kyma"
revision = "2ac7f3a71a873d0594f15c2309c032f4d42d6cdd"

[[constraint]]
name = "k8s.io/client-go"
version = "kubernetes-1.13.4"

[[override]]
name = "k8s.io/api"
version = "kubernetes-1.13.4"

[[override]]
name = "k8s.io/apimachinery"
version = "kubernetes-1.13.4"

# Kubebuilder dependencies
# STANZAS BELOW ARE GENERATED AND MAY BE WRITTEN - DO NOT MODIFY BELOW THIS LINE.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package accessservice

import (
"fmt"

"k8s.io/apimachinery/pkg/types"

"github.com/kyma-project/kyma/components/compass-runtime-agent/internal/apperrors"
"github.com/kyma-project/kyma/components/compass-runtime-agent/internal/k8sconsts"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

const appNameLabelFormat = "%s-application-gateway"

// ServiceInterface has methods to work with Service resources.
type ServiceInterface interface {
Create(*corev1.Service) (*corev1.Service, error)
Delete(name string, options *metav1.DeleteOptions) error
}

type AccessServiceManager interface {
Create(application string, appUID types.UID, serviceId, serviceName string) apperrors.AppError
Upsert(application string, appUID types.UID, serviceId, serviceName string) apperrors.AppError
Delete(serviceName string) apperrors.AppError
}

type AccessServiceManagerConfig struct {
TargetPort int32
}

type accessServiceManager struct {
serviceInterface ServiceInterface
config AccessServiceManagerConfig
}

func NewAccessServiceManager(serviceInterface ServiceInterface, config AccessServiceManagerConfig) AccessServiceManager {
return &accessServiceManager{
serviceInterface: serviceInterface,
config: config,
}
}

func (m *accessServiceManager) Create(application string, appUID types.UID, serviceId, serviceName string) apperrors.AppError {
_, err := m.create(application, appUID, serviceId, serviceName)
if err != nil {
return apperrors.Internal("Creating service failed, %s", err.Error())
}
return nil
}

func (m *accessServiceManager) Upsert(application string, appUID types.UID, serviceId, serviceName string) apperrors.AppError {
_, err := m.create(application, appUID, serviceId, serviceName)
if err != nil {
if k8serrors.IsAlreadyExists(err) {
return nil
}
return apperrors.Internal("Upserting service failed, %s", err.Error())
}
return nil
}

func (m *accessServiceManager) Delete(serviceName string) apperrors.AppError {
err := m.serviceInterface.Delete(serviceName, &metav1.DeleteOptions{})
if err != nil {
if !k8serrors.IsNotFound(err) {
return apperrors.Internal("Deleting service failed, %s", err.Error())
}
}
return nil
}

func (m *accessServiceManager) create(application string, appUID types.UID, serviceId, serviceName string) (*corev1.Service, error) {
appName := fmt.Sprintf(appNameLabelFormat, application)

service := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Labels: map[string]string{
k8sconsts.LabelApplication: application,
k8sconsts.LabelServiceId: serviceId,
},
OwnerReferences: k8sconsts.CreateOwnerReferenceForApplication(application, appUID),
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{k8sconsts.LabelApp: appName},
Ports: []corev1.ServicePort{
{
Name: "http",
Protocol: corev1.ProtocolTCP,
Port: 80,
TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: m.config.TargetPort},
},
},
},
}

return m.serviceInterface.Create(&service)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package accessservice

import (
"errors"
"testing"

"k8s.io/apimachinery/pkg/types"

"fmt"

"github.com/kyma-project/kyma/components/compass-runtime-agent/internal/apperrors"
"github.com/kyma-project/kyma/components/compass-runtime-agent/internal/k8sconsts"
"github.com/kyma-project/kyma/components/compass-runtime-agent/internal/synchronization/apiresources/accessservice/mocks"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
)

var config = AccessServiceManagerConfig{
TargetPort: 8081,
}

const (
applicationUID = types.UID("appUID")
)

func TestAccessServiceManager_Create(t *testing.T) {

t.Run("should create new access service", func(t *testing.T) {
// given
expectedService := mockService("ec-default", "uuid-1", "service-uuid1", 8081)
serviceInterface := new(mocks.ServiceInterface)
serviceInterface.On("Create", expectedService).Return(expectedService, nil)

manager := NewAccessServiceManager(serviceInterface, config)

// when
err := manager.Create("ec-default", "appUID", "uuid-1", "service-uuid1")

// then
assert.NoError(t, err)
serviceInterface.AssertExpectations(t)
})

t.Run("should handle errors", func(t *testing.T) {
// given
expectedService := mockService("ec-default", "uuid-1", "service-uuid1", 8081)
serviceInterface := new(mocks.ServiceInterface)
serviceInterface.On("Create", expectedService).Return(nil, errors.New("some error"))

manager := NewAccessServiceManager(serviceInterface, config)

// when
err := manager.Create("ec-default", "appUID", "uuid-1", "service-uuid1")

// then
assert.Error(t, err)
assert.Equal(t, apperrors.CodeInternal, err.Code())
assert.NotEmpty(t, err.Error())

serviceInterface.AssertExpectations(t)
})
}

func TestAccessServiceManager_Upsert(t *testing.T) {

t.Run("should create access service if not exists", func(t *testing.T) {
// given
expectedService := mockService("ec-default", "uuid-1", "service-uuid1", 8081)
serviceInterface := new(mocks.ServiceInterface)
serviceInterface.On("Create", expectedService).Return(expectedService, nil)

manager := NewAccessServiceManager(serviceInterface, config)

// when
err := manager.Upsert("ec-default", "appUID", "uuid-1", "service-uuid1")

// then
assert.NoError(t, err)
serviceInterface.AssertExpectations(t)
})

t.Run("should not fail if access service exists", func(t *testing.T) {
// given
expectedService := mockService("ec-default", "uuid-1", "service-uuid1", 8081)
serviceInterface := new(mocks.ServiceInterface)
serviceInterface.On("Create", expectedService).Return(nil, k8serrors.NewAlreadyExists(schema.GroupResource{}, ""))

manager := NewAccessServiceManager(serviceInterface, config)

// when
err := manager.Upsert("ec-default", "appUID", "uuid-1", "service-uuid1")

// then
assert.NoError(t, err)
serviceInterface.AssertExpectations(t)
})

t.Run("should handle errors", func(t *testing.T) {
// given
expectedService := mockService("ec-default", "uuid-1", "service-uuid1", 8081)
serviceInterface := new(mocks.ServiceInterface)
serviceInterface.On("Create", expectedService).Return(nil, errors.New("some error"))

manager := NewAccessServiceManager(serviceInterface, config)

// when
err := manager.Upsert("ec-default", "appUID", "uuid-1", "service-uuid1")

// then
assert.Error(t, err)
assert.Equal(t, apperrors.CodeInternal, err.Code())
assert.NotEmpty(t, err.Error())

serviceInterface.AssertExpectations(t)
})
}

func TestAccessServiceManager_Delete(t *testing.T) {

t.Run("should delete access service", func(t *testing.T) {
// given
serviceInterface := new(mocks.ServiceInterface)
serviceInterface.On("Delete", "test-service", &metav1.DeleteOptions{}).Return(nil)

manager := NewAccessServiceManager(serviceInterface, config)

// when
err := manager.Delete("test-service")

// then
assert.NoError(t, err)

serviceInterface.AssertExpectations(t)
})

t.Run("should handle errors", func(t *testing.T) {
// given
serviceInterface := new(mocks.ServiceInterface)
serviceInterface.On("Delete", "test-service", &metav1.DeleteOptions{}).Return(errors.New("some error"))

manager := NewAccessServiceManager(serviceInterface, config)

// when
err := manager.Delete("test-service")

// then
assert.Error(t, err)
assert.Equal(t, apperrors.CodeInternal, err.Code())
assert.NotEmpty(t, err.Error())

serviceInterface.AssertExpectations(t)
})

t.Run("should ignore not found error", func(t *testing.T) {
// given
serviceInterface := new(mocks.ServiceInterface)
serviceInterface.On("Delete", "test-service", &metav1.DeleteOptions{}).Return(k8serrors.NewNotFound(schema.GroupResource{}, "an error"))

manager := NewAccessServiceManager(serviceInterface, config)

// when
err := manager.Delete("test-service")

// then
assert.NoError(t, err)

serviceInterface.AssertExpectations(t)
})
}

func mockService(application, serviceId, serviceName string, targetPort int32) *corev1.Service {
appName := fmt.Sprintf(appNameLabelFormat, application)

return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Labels: map[string]string{
k8sconsts.LabelApplication: application,
k8sconsts.LabelServiceId: serviceId,
},
OwnerReferences: k8sconsts.CreateOwnerReferenceForApplication(application, applicationUID),
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
k8sconsts.LabelApp: appName,
},
Ports: []corev1.ServicePort{
{
Name: "http",
Protocol: corev1.ProtocolTCP,
Port: 80,
TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: targetPort},
},
},
},
}
}

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

Loading