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

Initial DB Table #1213

Merged
merged 39 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
bc9e5c7
Initial DB Table
ralikio Sep 27, 2024
b53c8ff
Merge branch 'main' into features/bindings-schema
MarekMichali Sep 30, 2024
ec82659
Structures Extension
ralikio Oct 2, 2024
7529a45
Linter
ralikio Oct 2, 2024
5226ce0
Merge branch 'main' into features/bindings-schema
ralikio Oct 2, 2024
8bd59ab
DAO Implementation
ralikio Oct 2, 2024
80f13f4
Removed Commented Out Consts
ralikio Oct 2, 2024
dd28953
Unnecessary Filter
ralikio Oct 2, 2024
e92d7f3
Linter
ralikio Oct 2, 2024
0569b73
Attributes Correction
ralikio Oct 3, 2024
4675439
DAO Test
ralikio Oct 3, 2024
f399a99
Typo Correction
ralikio Oct 3, 2024
f7dfda1
Corrected Scripts Names and Added RuntimeID Index
ralikio Oct 3, 2024
2cd1d3c
Merge branch 'main' into features/bindings-schema
ralikio Oct 3, 2024
f58669a
Changed Runtime Id to Instance Id
ralikio Oct 3, 2024
ad98b31
Corrected Param Value in Example Token Request
ralikio Oct 3, 2024
0b3ae32
Removed Old Retry
ralikio Oct 3, 2024
0a36b1b
Formatting Corrections
ralikio Oct 3, 2024
c70baa4
Linter
ralikio Oct 3, 2024
b3a1dd6
Spacing Correction
ralikio Oct 3, 2024
74cfcc4
Removed Unused Mocks Generation
ralikio Oct 3, 2024
1512388
Additional Test Cases
ralikio Oct 3, 2024
3509cb0
Removed Redundant Version Field
ralikio Oct 3, 2024
8acc335
Merge branch 'main' into features/bindings-schema
ralikio Oct 3, 2024
8879731
Corrected Test Case
ralikio Oct 3, 2024
7292b02
Merge branch 'main' into features/bindings-schema
ralikio Oct 4, 2024
5772874
Corrected Text to Varchar for Type Field
ralikio Oct 4, 2024
e75005a
Corrected Type Field
ralikio Oct 4, 2024
5e34a92
Missed runtimeID Correction
ralikio Oct 4, 2024
edb1b71
Review Remarks
ralikio Oct 4, 2024
6c65932
Naming Corrections
ralikio Oct 4, 2024
3187ef4
Review Remarks
ralikio Oct 4, 2024
4cf8bde
Compilation Correction
ralikio Oct 4, 2024
1abc338
Compilation Correction
ralikio Oct 4, 2024
77d1d8c
ListByInstanceID
ralikio Oct 4, 2024
4705e75
Merge branch 'main' into features/bindings-schema
piotrmiskiewicz Oct 4, 2024
e9af6b5
Linter
ralikio Oct 4, 2024
0aafb19
Corrected Const Name
ralikio Oct 4, 2024
c66a84b
Corrected Const Name
ralikio Oct 4, 2024
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
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_TOKEN_REQUEST,
}
}

// 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_TOKEN_REQUEST = "token_request"
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
Loading