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

configurator: simplify MeshConfig API #4475

Merged
merged 1 commit into from
Jan 25, 2022
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
12 changes: 6 additions & 6 deletions pkg/configurator/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ func (c *client) getMeshConfigCacheKey() string {
}

// Returns the current MeshConfig
func (c *client) getMeshConfig() *configv1alpha2.MeshConfig {
func (c *client) getMeshConfig() configv1alpha2.MeshConfig {
var meshConfig configv1alpha2.MeshConfig

meshConfigCacheKey := c.getMeshConfigCacheKey()
item, exists, err := c.cache.GetByKey(meshConfigCacheKey)
if err != nil {
log.Error().Err(err).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrMeshConfigFetchFromCache)).Msgf("Error getting MeshConfig from cache with key %s", meshConfigCacheKey)
return &configv1alpha2.MeshConfig{}
return meshConfig
}

var meshConfig *configv1alpha2.MeshConfig
if !exists {
log.Warn().Msgf("MeshConfig %s does not exist. Default config values will be used.", meshConfigCacheKey)
meshConfig = &configv1alpha2.MeshConfig{}
} else {
meshConfig = item.(*configv1alpha2.MeshConfig)
return meshConfig
}

meshConfig = *item.(*configv1alpha2.MeshConfig)
return meshConfig
}
4 changes: 2 additions & 2 deletions pkg/configurator/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGetMeshConfig(t *testing.T) {
c := newConfigurator(meshConfigClient, stop, osmNamespace, osmMeshConfigName, nil)

// Returns empty MeshConfig if informer cache is empty
a.Equal(&configv1alpha2.MeshConfig{}, c.getMeshConfig())
a.Equal(configv1alpha2.MeshConfig{}, c.getMeshConfig())

newObj := &configv1alpha2.MeshConfig{
TypeMeta: metav1.TypeMeta{
Expand All @@ -38,5 +38,5 @@ func TestGetMeshConfig(t *testing.T) {
}
err := c.cache.Add(newObj)
a.Nil(err)
a.Equal(newObj, c.getMeshConfig())
a.Equal(*newObj, c.getMeshConfig())
}
8 changes: 4 additions & 4 deletions pkg/configurator/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
// The functions in this file implement the configurator.Configurator interface

// GetMeshConfig returns the MeshConfig resource corresponding to the control plane
func (c *client) GetMeshConfig() *configv1alpha2.MeshConfig {
func (c *client) GetMeshConfig() configv1alpha2.MeshConfig {
return c.getMeshConfig()
}

Expand All @@ -41,8 +41,8 @@ func (c *client) GetOSMNamespace() string {
return c.osmNamespace
}

func marshalConfigToJSON(config *configv1alpha2.MeshConfigSpec) (string, error) {
bytes, err := json.MarshalIndent(config, "", " ")
func marshalConfigToJSON(config configv1alpha2.MeshConfigSpec) (string, error) {
bytes, err := json.MarshalIndent(&config, "", " ")
if err != nil {
return "", err
}
Expand All @@ -51,7 +51,7 @@ func marshalConfigToJSON(config *configv1alpha2.MeshConfigSpec) (string, error)

// GetMeshConfigJSON returns the MeshConfig in pretty JSON.
func (c *client) GetMeshConfigJSON() (string, error) {
cm, err := marshalConfigToJSON(&c.getMeshConfig().Spec)
cm, err := marshalConfigToJSON(c.getMeshConfig().Spec)
if err != nil {
log.Error().Err(err).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrMeshConfigMarshaling)).Msgf("Error marshaling MeshConfig %s: %+v", c.getMeshConfigCacheKey(), c.getMeshConfig())
return "", err
Expand Down
4 changes: 2 additions & 2 deletions pkg/configurator/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCreateUpdateConfig(t *testing.T) {

stop := make(chan struct{})
cfg := newConfigurator(meshConfigClientSet, stop, osmNamespace, osmMeshConfigName, nil)
tassert.Equal(t, &configv1alpha2.MeshConfig{}, cfg.getMeshConfig())
tassert.Equal(t, configv1alpha2.MeshConfig{}, cfg.getMeshConfig())
})

tests := []struct {
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestCreateUpdateConfig(t *testing.T) {
ServiceCertValidityDuration: "24h",
},
}
expectedConfigJSON, err := marshalConfigToJSON(expectedConfig)
expectedConfigJSON, err := marshalConfigToJSON(*expectedConfig)
assert.Nil(err)

configJSON, err := cfg.GetMeshConfigJSON()
Expand Down
4 changes: 2 additions & 2 deletions pkg/configurator/mock_client_generated.go

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

2 changes: 1 addition & 1 deletion pkg/configurator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type client struct {
// Configurator is the controller interface for K8s namespaces
type Configurator interface {
// GetMeshConfig returns the MeshConfig resource corresponding to the control plane
GetMeshConfig() *configv1alpha2.MeshConfig
GetMeshConfig() configv1alpha2.MeshConfig

// GetOSMNamespace returns the namespace in which OSM controller pod resides
GetOSMNamespace() string
Expand Down
7 changes: 1 addition & 6 deletions pkg/ingress/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ import (
// 2. Starts a goroutine to watch for changes to the MeshConfig resource and certificate rotation, and
// updates/removes the certificate and secret as necessary.
func (c *client) provisionIngressGatewayCert(stop <-chan struct{}) error {
meshConfig := c.cfg.GetMeshConfig()
if meshConfig == nil {
return errors.New("MeshConfig cannot be nil")
}

defaultCertSpec := meshConfig.Spec.Certificate.IngressGateway
defaultCertSpec := c.cfg.GetMeshConfig().Spec.Certificate.IngressGateway
if defaultCertSpec != nil {
// Issue a certificate for the default certificate spec
if err := c.createAndStoreGatewayCert(*defaultCertSpec); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/ingress/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func TestProvisionIngressGatewayCert(t *testing.T) {

testCases := []struct {
name string
meshConfig *configv1alpha2.MeshConfig
meshConfig configv1alpha2.MeshConfig
expectSecretToExist bool
expectErr bool
}{
{
name: "ingress gateway cert spec does not exist",
meshConfig: &configv1alpha2.MeshConfig{
meshConfig: configv1alpha2.MeshConfig{
Spec: configv1alpha2.MeshConfigSpec{
Certificate: configv1alpha2.CertificateSpec{
IngressGateway: nil,
Expand All @@ -56,7 +56,7 @@ func TestProvisionIngressGatewayCert(t *testing.T) {
},
{
name: "ingress gateway cert spec exists",
meshConfig: &configv1alpha2.MeshConfig{
meshConfig: configv1alpha2.MeshConfig{
Spec: configv1alpha2.MeshConfigSpec{
Certificate: configv1alpha2.CertificateSpec{
IngressGateway: &configv1alpha2.IngressGatewayCertSpec{
Expand All @@ -72,7 +72,7 @@ func TestProvisionIngressGatewayCert(t *testing.T) {
},
{
name: "ingress gateway cert spec has no SAN",
meshConfig: &configv1alpha2.MeshConfig{
meshConfig: configv1alpha2.MeshConfig{
Spec: configv1alpha2.MeshConfigSpec{
Certificate: configv1alpha2.CertificateSpec{
IngressGateway: &configv1alpha2.IngressGatewayCertSpec{
Expand Down