Skip to content

Commit

Permalink
Initial DB Table (#1213)
Browse files Browse the repository at this point in the history
* Initial DB Table

* Structures Extension

* Linter

* DAO Implementation

* Removed Commented Out Consts

* Unnecessary Filter

* Linter

* Attributes Correction

* DAO Test

* Typo Correction

* Corrected Scripts Names and Added RuntimeID Index

* Changed Runtime Id to Instance Id

During code review I received input that instance id is the main identity value for SKRs in KEB.

* Corrected Param Value in Example Token Request

* Removed Old Retry

* Formatting Corrections

* Linter

* Spacing Correction

* Removed Unused Mocks Generation

* Additional Test Cases

* Removed Redundant Version Field

* Corrected Test Case

* Corrected Text to Varchar for Type Field

* Corrected Type Field

* Missed runtimeID Correction

* Review Remarks

* Naming Corrections

* Review Remarks

* Compilation Correction

* Compilation Correction

* ListByInstanceID

* Linter

* Corrected Const Name

* Corrected Const Name

---------

Co-authored-by: Marek Michali <56163696+MarekMichali@users.noreply.github.com>
Co-authored-by: Piotr Miśkiewicz <piotr.miskiewicz@sap.com>
  • Loading branch information
3 people authored Oct 4, 2024
1 parent d4ddae9 commit 1947428
Show file tree
Hide file tree
Showing 15 changed files with 492 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,27 @@ func FixRuntimeState(id, runtimeID, operationID string) internal.RuntimeState {
}
}

func FixBinding(id string) internal.Binding {
var instanceID = fmt.Sprintf("instance-%s", id)

return FixBindingWithInstanceID(id, instanceID)
}

func FixBindingWithInstanceID(bindingID string, instanceID string) internal.Binding {
return internal.Binding{
ID: bindingID,
InstanceID: instanceID,

CreatedAt: time.Now(),
UpdatedAt: time.Now().Add(time.Minute * 5),

Kubeconfig: "kubeconfig",
ExpirationSeconds: 600,
GenerationMethod: "adminkubeconfig",
BindingType: internal.BINDING_TYPE_SERVICE_ACCOUNT,
}
}

// SimpleInputCreator implements ProvisionerInputCreator interface
func (c *SimpleInputCreator) SetProvisioningParameters(params internal.ProvisioningParameters) internal.ProvisionerInputCreator {
return c
Expand Down
16 changes: 16 additions & 0 deletions internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
log "github.com/sirupsen/logrus"
)

const BINDING_TYPE_SERVICE_ACCOUNT = "service_account"
const BINDING_TYPE_ADMIN_KUBECONFIG = "gardener_admin_kubeconfig"

type ProvisionerInputCreator interface {
SetProvisioningParameters(params ProvisioningParameters) ProvisionerInputCreator
SetShootName(string) ProvisionerInputCreator
Expand Down Expand Up @@ -579,3 +582,16 @@ type DeletedStats struct {
NumberOfDeletedInstances int
NumberOfOperationsForDeletedInstances int
}

type Binding struct {
ID string
InstanceID string

CreatedAt time.Time
UpdatedAt time.Time

Kubeconfig string
ExpirationSeconds int64
GenerationMethod string
BindingType string
}
17 changes: 17 additions & 0 deletions internal/storage/dbmodel/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dbmodel

import (
"time"
)

type BindingDTO struct {
ID string
InstanceID string

CreatedAt time.Time

Kubeconfig string
ExpirationSeconds int64
GenerationMethod string
BindingType string
}
114 changes: 114 additions & 0 deletions internal/storage/driver/postsql/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package postsql

import (
"fmt"

"github.com/kyma-project/kyma-environment-broker/internal"
"github.com/kyma-project/kyma-environment-broker/internal/storage/dberr"
"github.com/kyma-project/kyma-environment-broker/internal/storage/dbmodel"
"github.com/kyma-project/kyma-environment-broker/internal/storage/postsql"
log "github.com/sirupsen/logrus"
)

type Binding struct {
postsql.Factory
cipher Cipher
}

func NewBinding(sess postsql.Factory, cipher Cipher) *Binding {
return &Binding{
Factory: sess,
cipher: cipher,
}
}

func (s *Binding) GetByBindingID(bindingId string) (*internal.Binding, error) {
sess := s.NewReadSession()
bindingDTO := dbmodel.BindingDTO{}
bindingDTO, lastErr := sess.GetBindingByID(bindingId)
if lastErr != nil {
if dberr.IsNotFound(lastErr) {
return nil, dberr.NotFound("Binding with id %s not exist", bindingId)
}
log.Errorf("while getting instanceDTO by ID %s: %v", bindingId, lastErr)
return nil, lastErr
}
binding, err := s.toBinding(bindingDTO)
if err != nil {
return nil, err
}

return &binding, nil
}

func (s *Binding) Insert(binding *internal.Binding) error {
_, err := s.GetByBindingID(binding.ID)
if err == nil {
return dberr.AlreadyExists("instance with id %s already exist", binding.ID)
}

dto, err := s.toBindingDTO(binding)
if err != nil {
return err
}

sess := s.NewWriteSession()
err = sess.InsertBinding(dto)
if err != nil {
return fmt.Errorf("while saving binding with ID %s: %w", binding.ID, err)
}

return nil
}

func (s *Binding) DeleteByBindingID(ID string) error {
sess := s.NewWriteSession()
return sess.DeleteBinding(ID)
}

func (s *Binding) ListByInstanceID(instanceID string) ([]internal.Binding, error) {
dtos, err := s.NewReadSession().ListBindings(instanceID)
if err != nil {
return []internal.Binding{}, err
}
var bindings []internal.Binding
for _, dto := range dtos {
instance, err := s.toBinding(dto)
if err != nil {
return []internal.Binding{}, err
}

bindings = append(bindings, instance)
}
return bindings, err
}

func (s *Binding) toBindingDTO(binding *internal.Binding) (dbmodel.BindingDTO, error) {
encrypted, err := s.cipher.Encrypt([]byte(binding.Kubeconfig))
if err != nil {
return dbmodel.BindingDTO{}, fmt.Errorf("while encrypting kubeconfig: %w", err)
}

return dbmodel.BindingDTO{
Kubeconfig: string(encrypted),
ID: binding.ID,
InstanceID: binding.InstanceID,
CreatedAt: binding.CreatedAt,
ExpirationSeconds: binding.ExpirationSeconds,
}, nil
}

func (s *Binding) toBinding(dto dbmodel.BindingDTO) (internal.Binding, error) {
decrypted, err := s.cipher.Decrypt([]byte(dto.Kubeconfig))
if err != nil {
return internal.Binding{}, fmt.Errorf("while decrypting kubeconfig: %w", err)
}

return internal.Binding{
Kubeconfig: string(decrypted),
ID: dto.ID,
InstanceID: dto.InstanceID,
CreatedAt: dto.CreatedAt,
ExpirationSeconds: dto.ExpirationSeconds,
}, nil
}
Loading

0 comments on commit 1947428

Please sign in to comment.