Skip to content

Commit

Permalink
refactor: delete custom entities
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Dec 15, 2022
1 parent 6e0bb17 commit 8690132
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 182 deletions.
18 changes: 4 additions & 14 deletions internal/dataplane/deckgen/deckgen.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package deckgen

import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
Expand All @@ -10,24 +9,15 @@ import (
"github.com/kong/go-kong/kong"
)

// GenerateSHA generates a SHA256 checksum of the (targetContent, customEntities) tuple, with the purpose of change
// detection.
func GenerateSHA(targetContent *file.Content,
customEntities []byte,
) ([]byte, error) {
var buffer bytes.Buffer

// GenerateSHA generates a SHA256 checksum of targetContent, with the purpose
// of change detection.
func GenerateSHA(targetContent *file.Content) ([]byte, error) {
jsonConfig, err := json.Marshal(targetContent)
if err != nil {
return nil, fmt.Errorf("marshaling Kong declarative configuration to JSON: %w", err)
}
buffer.Write(jsonConfig)

if customEntities != nil {
buffer.Write(customEntities)
}

shaSum := sha256.Sum256(buffer.Bytes())
shaSum := sha256.Sum256(jsonConfig)
return shaSum[:], nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/dataplane/kong_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ func (c *KongClient) Update(ctx context.Context) error {
c.skipCACertificates,
targetConfig,
c.kongConfig.FilterTags,
nil,
c.lastConfigSHA,
c.prometheusMetrics,
)
Expand Down
60 changes: 4 additions & 56 deletions internal/dataplane/sendconfig/sendconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const initialHash = "00000000000000000000000000000000"
// Sendconfig - Public Functions
// -----------------------------------------------------------------------------

// PerformUpdate writes `targetContent` and `customEntities` to Kong Admin API specified by `kongConfig`.
// PerformUpdate writes `targetContent` to Kong Admin API specified by `kongConfig`.
func PerformUpdate(ctx context.Context,
log logrus.FieldLogger,
kongConfig *Kong,
Expand All @@ -41,11 +41,10 @@ func PerformUpdate(ctx context.Context,
skipCACertificates bool,
targetContent *file.Content,
selectorTags []string,
customEntities []byte,
oldSHA []byte,
promMetrics *metrics.CtrlFuncMetrics,
) ([]byte, error) {
newSHA, err := deckgen.GenerateSHA(targetContent, customEntities)
newSHA, err := deckgen.GenerateSHA(targetContent)
if err != nil {
return oldSHA, err
}
Expand Down Expand Up @@ -79,7 +78,7 @@ func PerformUpdate(ctx context.Context,
timeStart := time.Now()
if inMemory {
metricsProtocol = metrics.ProtocolDBLess
err = onUpdateInMemoryMode(ctx, log, targetContent, customEntities, kongConfig)
err = onUpdateInMemoryMode(ctx, log, targetContent, kongConfig)
} else {
metricsProtocol = metrics.ProtocolDeck
err = onUpdateDBMode(ctx, targetContent, kongConfig, selectorTags, skipCACertificates)
Expand Down Expand Up @@ -116,68 +115,17 @@ func PerformUpdate(ctx context.Context,
// Sendconfig - Private Functions
// -----------------------------------------------------------------------------

func renderConfigWithCustomEntities(log logrus.FieldLogger, state *file.Content,
customEntitiesJSONBytes []byte,
) ([]byte, error) {
var kongCoreConfig []byte
var err error

kongCoreConfig, err = json.Marshal(state)
if err != nil {
return nil, fmt.Errorf("marshaling kong config into json: %w", err)
}

// fast path
if len(customEntitiesJSONBytes) == 0 {
return kongCoreConfig, nil
}

// slow path
mergeMap := map[string]interface{}{}
var result []byte
var customEntities map[string]interface{}

// unmarshal core config into the merge map
err = json.Unmarshal(kongCoreConfig, &mergeMap)
if err != nil {
return nil, fmt.Errorf("unmarshalling kong config into map[string]interface{}: %w", err)
}

// unmarshal custom entities config into the merge map
err = json.Unmarshal(customEntitiesJSONBytes, &customEntities)
if err != nil {
// do not error out when custom entities are messed up
log.WithError(err).Error("failed to unmarshal custom entities from secret data")
} else {
for k, v := range customEntities {
if _, exists := mergeMap[k]; !exists {
mergeMap[k] = v
}
}
}

// construct the final configuration
result, err = json.Marshal(mergeMap)
if err != nil {
err = fmt.Errorf("marshaling final config into JSON: %w", err)
return nil, err
}

return result, nil
}

func onUpdateInMemoryMode(ctx context.Context,
log logrus.FieldLogger,
state *file.Content,
customEntities []byte,
kongConfig *Kong,
) error {
// Kong will error out if this is set
state.Info = nil
// Kong errors out if `null`s are present in `config` of plugins
deckgen.CleanUpNullsInPluginConfigs(state)

config, err := renderConfigWithCustomEntities(log, state, customEntities)
config, err := json.Marshal(state)
if err != nil {
return fmt.Errorf("constructing kong configuration: %w", err)
}
Expand Down
111 changes: 0 additions & 111 deletions internal/dataplane/sendconfig/sendconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,127 +5,16 @@ import (
"fmt"
"net"
"net/http"
"reflect"
"testing"

"github.com/kong/deck/file"
deckutils "github.com/kong/deck/utils"
"github.com/kong/go-kong/kong"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/kong/kubernetes-ingress-controller/v2/internal/metrics"
)

func TestRenderConfigWithCustomEntities(t *testing.T) {
type args struct {
state *file.Content
customEntitiesJSONBytes []byte
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
name: "basic sanity test for fast-path",
args: args{
state: &file.Content{
FormatVersion: "1.1",
Services: []file.FService{
{
Service: kong.Service{
Name: kong.String("foo"),
Host: kong.String("example.com"),
},
},
},
},
customEntitiesJSONBytes: nil,
},
want: []byte(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`),
wantErr: false,
},
{
name: "does not break with random bytes in the custom entities",
args: args{
state: &file.Content{
FormatVersion: "1.1",
Services: []file.FService{
{
Service: kong.Service{
Name: kong.String("foo"),
Host: kong.String("example.com"),
},
},
},
},
customEntitiesJSONBytes: []byte("random-bytes"),
},
want: []byte(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`),
wantErr: false,
},
{
name: "custom entities cannot hijack core entities",
args: args{
state: &file.Content{
FormatVersion: "1.1",
Services: []file.FService{
{
Service: kong.Service{
Name: kong.String("foo"),
Host: kong.String("example.com"),
},
},
},
},
customEntitiesJSONBytes: []byte(`{"services":[{"host":"rogue.example.com","name":"rogue"}]}`),
},
want: []byte(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`),
wantErr: false,
},
{
name: "custom entities can be populated",
args: args{
state: &file.Content{
FormatVersion: "1.1",
Services: []file.FService{
{
Service: kong.Service{
Name: kong.String("foo"),
Host: kong.String("example.com"),
},
},
},
},
customEntitiesJSONBytes: []byte(`{"my-custom-dao-name":` +
`[{"name":"custom1","key1":"value1"},` +
`{"name":"custom2","dumb":"test-value","boring-test-value-name":"really?"}]}`),
},
want: []byte(`{"_format_version":"1.1",` +
`"my-custom-dao-name":[{"key1":"value1","name":"custom1"},` +
`{"boring-test-value-name":"really?","dumb":"test-value","name":"custom2"}]` +
`,"services":[{"host":"example.com","name":"foo"}]}`),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := renderConfigWithCustomEntities(logrus.New(), tt.args.state, tt.args.customEntitiesJSONBytes)
if (err != nil) != tt.wantErr {
t.Errorf("renderConfigWithCustomEntities() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("renderConfigWithCustomEntities() = %v, want %v",
string(got), string(tt.want))
}
})
}
}

func TestUpdateReportingUtilities(t *testing.T) {
assert.False(t, hasSHAUpdateAlreadyBeenReported([]byte("fake-sha")))
assert.True(t, hasSHAUpdateAlreadyBeenReported([]byte("fake-sha")))
Expand Down

0 comments on commit 8690132

Please sign in to comment.