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

cleanup: Rename CustomSearchDomain to CustomSearchDomainConfig #4296

Merged
merged 27 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
344b99b
polishing contract
Apr 10, 2024
013a1a9
minor update
Apr 10, 2024
3de3b8d
remove extra empty lines
Apr 10, 2024
584b485
fix indents
Apr 10, 2024
86b1f83
Merge branch 'master' into devinwon/polish_contract
Devinwong Apr 10, 2024
3d3406f
Merge branch 'master' into devinwon/polish_contract
Devinwong Apr 10, 2024
2906ae1
remove optional from disable_outbound_snat
Apr 10, 2024
a04c1ef
Merge branch 'devinwon/polish_contract' of https://github.com/Azure/A…
Apr 10, 2024
d37c16b
Rename CustomSearchDomain to CustomSearchDomainConfig
Apr 15, 2024
c84268d
Merge branch 'devinwon/polish_contract' into devinwon/CustomSearchDomain
Apr 15, 2024
aeff7e6
Add NewNBContractConfiguration in utlities
Apr 16, 2024
8ffdab4
move NewNBContractConfiguration to nbcontracthelper package
Apr 16, 2024
3bcb719
add comment
Apr 16, 2024
6ff5578
Refactor NBContractConfig initialization and configuration handling
Apr 16, 2024
c8982ae
rename to NBContractBuilder
Apr 16, 2024
7a36bf0
Merge branch 'master' into devinwon/CustomSearchDomain
Devinwong Apr 16, 2024
d55d219
add deepCopy
Apr 16, 2024
f8d4f55
Merge branch 'devinwon/CustomSearchDomain' of https://github.com/Azur…
Apr 16, 2024
664cedd
make ensureConfigsNonNil local in order to reuse it
Apr 16, 2024
ea5a8be
rename nBContractConfiguration to nodeBootstrapConfig
Apr 16, 2024
a87f186
init next level configs
Apr 16, 2024
6a3cb25
Merge branch 'master' into devinwon/CustomSearchDomain
Devinwong Apr 16, 2024
20a1fc8
revert changes to go.mod and go.sum
Apr 16, 2024
ab7afb0
revert go.mod
Apr 16, 2024
9cec429
cleaning up lint
Apr 16, 2024
8b3b7f5
Fix lint
Apr 16, 2024
9fe2166
fix lint
Apr 16, 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
85 changes: 81 additions & 4 deletions pkg/nbcontracthelper/helper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,86 @@
package nbcontracthelper

const (
defaultLinuxUser = "azureuser"
import (
"bytes"
"encoding/gob"
"log"

nbcontractv1 "github.com/Azure/agentbaker/pkg/proto/nbcontract/v1"
)

func GetDefaultLinuxAdminUsername() string {
return defaultLinuxUser
type NBContractBuilder struct {
// nodeBootstrapConfig is the configuration object for the NBContract (Node Bootstrap Contract).
nodeBootstrapConfig *nbcontractv1.Configuration
}

// Check and initialize each field if it is nil.
func initializeIfNil[T any](field **T) {
if *field == nil {
*field = new(T)
}
}

// Ensure all objects are non-nil. Please add new objects here.
func ensureConfigsNonNil(nBC *nbcontractv1.Configuration) {
initializeIfNil(&nBC.KubeBinaryConfig)
initializeIfNil(&nBC.ApiServerConfig)
initializeIfNil(&nBC.AuthConfig)
initializeIfNil(&nBC.ClusterConfig)
initializeIfNil(&nBC.ClusterConfig.LoadBalancerConfig)
initializeIfNil(&nBC.ClusterConfig.ClusterNetworkConfig)
initializeIfNil(&nBC.NetworkConfig)
initializeIfNil(&nBC.GpuConfig)
initializeIfNil(&nBC.TlsBootstrappingConfig)
initializeIfNil(&nBC.KubeletConfig)
initializeIfNil(&nBC.RuncConfig)
initializeIfNil(&nBC.ContainerdConfig)
initializeIfNil(&nBC.TeleportConfig)
initializeIfNil(&nBC.CustomLinuxOsConfig)
initializeIfNil(&nBC.HttpProxyConfig)
initializeIfNil(&nBC.CustomCloudConfig)
initializeIfNil(&nBC.CustomSearchDomainConfig)
}

// Creates a new instance of NBContractBuilder and ensures all objects in nodeBootstrapConfig are non-nil.
func NewNBContractBuilder() *NBContractBuilder {
nbc := &nbcontractv1.Configuration{}
ensureConfigsNonNil(nbc)
nBCB := &NBContractBuilder{nodeBootstrapConfig: nbc}
return nBCB
}

// Apply the configuration to the nodeBootstrapConfig object.
func (nBCB *NBContractBuilder) ApplyConfiguration(config *nbcontractv1.Configuration) {
if config == nil {
return
}

// Use deep copy to avoid modifying the original object 'config'.
if err := nBCB.deepCopy(config, nBCB.nodeBootstrapConfig); err != nil {
log.Printf("Failed to deep copy the configuration: %v", err)
ensureConfigsNonNil(nBCB.nodeBootstrapConfig)
}
}

// Get the nodeBootstrapConfig object.
func (nBCB *NBContractBuilder) GetNodeBootstrapConfig() *nbcontractv1.Configuration {
return nBCB.nodeBootstrapConfig
}

// Deep copy the source object to the destination object.
// Note that the existing value in the destination object will not be cleared
// if the source object doesn't have that field.
func (nBCB *NBContractBuilder) deepCopy(src, dst interface{}) error {
if src == nil {
return nil
}

var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
return err
}
if err := gob.NewDecoder(&buf).Decode(dst); err != nil {
return err
}
return nil
}
224 changes: 224 additions & 0 deletions pkg/nbcontracthelper/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package nbcontracthelper

import (
"log"
"reflect"
"testing"

nbcontractv1 "github.com/Azure/agentbaker/pkg/proto/nbcontract/v1"
"github.com/Azure/go-autorest/autorest/to"
)

func TestNewNBContractBuilder(t *testing.T) {
wantedResult := &nbcontractv1.Configuration{
KubeBinaryConfig: &nbcontractv1.KubeBinaryConfig{},
ApiServerConfig: &nbcontractv1.ApiServerConfig{},
AuthConfig: &nbcontractv1.AuthConfig{},
ClusterConfig: &nbcontractv1.ClusterConfig{
LoadBalancerConfig: &nbcontractv1.LoadBalancerConfig{},
ClusterNetworkConfig: &nbcontractv1.ClusterNetworkConfig{},
},
NetworkConfig: &nbcontractv1.NetworkConfig{},
GpuConfig: &nbcontractv1.GPUConfig{},
TlsBootstrappingConfig: &nbcontractv1.TLSBootstrappingConfig{},
KubeletConfig: &nbcontractv1.KubeletConfig{},
RuncConfig: &nbcontractv1.RuncConfig{},
ContainerdConfig: &nbcontractv1.ContainerdConfig{},
TeleportConfig: &nbcontractv1.TeleportConfig{},
CustomLinuxOsConfig: &nbcontractv1.CustomLinuxOSConfig{},
HttpProxyConfig: &nbcontractv1.HTTPProxyConfig{},
CustomCloudConfig: &nbcontractv1.CustomCloudConfig{},
CustomSearchDomainConfig: &nbcontractv1.CustomSearchDomainConfig{},
}
tests := []struct {
name string
want *nbcontractv1.Configuration
}{
{
name: "Test with nil configuration",
want: wantedResult,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewNBContractBuilder().nodeBootstrapConfig; !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewNBContractConfiguration() = %v, want %v", got, tt.want)
}
})
}
}

func TestNBContractBuilder_ApplyConfiguration(t *testing.T) {
type fields struct {
nBContractConfiguration *nbcontractv1.Configuration
}
wantedResult := &nbcontractv1.Configuration{
KubeBinaryConfig: &nbcontractv1.KubeBinaryConfig{},
ApiServerConfig: &nbcontractv1.ApiServerConfig{},
AuthConfig: &nbcontractv1.AuthConfig{},
ClusterConfig: &nbcontractv1.ClusterConfig{
LoadBalancerConfig: &nbcontractv1.LoadBalancerConfig{},
ClusterNetworkConfig: &nbcontractv1.ClusterNetworkConfig{},
},
NetworkConfig: &nbcontractv1.NetworkConfig{},
GpuConfig: &nbcontractv1.GPUConfig{},
TlsBootstrappingConfig: &nbcontractv1.TLSBootstrappingConfig{},
KubeletConfig: &nbcontractv1.KubeletConfig{},
RuncConfig: &nbcontractv1.RuncConfig{},
ContainerdConfig: &nbcontractv1.ContainerdConfig{},
TeleportConfig: &nbcontractv1.TeleportConfig{},
CustomLinuxOsConfig: &nbcontractv1.CustomLinuxOSConfig{},
HttpProxyConfig: &nbcontractv1.HTTPProxyConfig{},
CustomCloudConfig: &nbcontractv1.CustomCloudConfig{},
CustomSearchDomainConfig: &nbcontractv1.CustomSearchDomainConfig{},
}
tests := []struct {
name string
fields fields
want *nbcontractv1.Configuration
}{
{
name: "Test with nil configuration",
fields: fields{
nBContractConfiguration: &nbcontractv1.Configuration{},
},
want: wantedResult,
},
{
name: "Apply nil AuthConfig configuration and expect AuthConfig in nBContractConfiguration to be non-nil",
fields: fields{
nBContractConfiguration: &nbcontractv1.Configuration{
AuthConfig: nil,
},
},
want: wantedResult,
},
{
name: "Apply some configurations and expect them to be applied",
fields: fields{
nBContractConfiguration: &nbcontractv1.Configuration{
AuthConfig: &nbcontractv1.AuthConfig{
TargetCloud: "some-cloud",
},
LinuxAdminUsername: "testuser",
},
},
want: func() *nbcontractv1.Configuration {
tmpResult := NewNBContractBuilder().nodeBootstrapConfig
tmpResult.AuthConfig.TargetCloud = "some-cloud"
tmpResult.LinuxAdminUsername = "testuser"
return tmpResult
}(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
builder := NewNBContractBuilder()
builder.ApplyConfiguration(tt.fields.nBContractConfiguration)
if got := builder.nodeBootstrapConfig; !reflect.DeepEqual(got, tt.want) {
t.Errorf("ApplyConfiguration() = %v, want %v", got, tt.want)
}
})
}
}

func TestNBContractBuilder_deepCopy(t *testing.T) {
type Teststruct struct {
A string
B *int
C bool
}
type args struct {
src *Teststruct
dst *Teststruct
}
tests := []struct {
name string
args args
want *Teststruct
isChangeDst bool
}{
{
name: "Test with empty src",
args: args{
src: &Teststruct{},
dst: &Teststruct{},
},
want: &Teststruct{},
isChangeDst: false,
},
{
name: "Test with non-empty src",
args: args{
src: &Teststruct{
A: "some-string",
B: to.IntPtr(123),
C: true,
},
dst: &Teststruct{},
},
want: &Teststruct{
A: "some-string",
B: to.IntPtr(123),
C: true,
},
isChangeDst: false,
},
{
name: "Test with dst which has some existing values. Expect them to be overwritten",
args: args{
src: &Teststruct{
A: "some-string",
B: to.IntPtr(123),
},
dst: &Teststruct{
A: "some-other-string",
B: to.IntPtr(456),
C: false,
},
},
want: &Teststruct{
A: "some-string",
B: to.IntPtr(123),
C: false,
},
isChangeDst: false,
},
{
name: "After deepCopy, changes in dst should not affect src",
args: args{
src: &Teststruct{
A: "some-string",
B: to.IntPtr(123),
},
dst: &Teststruct{},
},
want: &Teststruct{
A: "some-string",
B: to.IntPtr(123),
},
isChangeDst: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nBCB := &NBContractBuilder{
nodeBootstrapConfig: &nbcontractv1.Configuration{},
}
if err := nBCB.deepCopy(tt.args.src, tt.args.dst); err != nil {
log.Printf("Failed to deep copy the configuration: %v", err)
}
log.Printf("dst = %v, src %v", tt.args.dst, tt.args.src)
if got := tt.args.dst; !reflect.DeepEqual(got, tt.want) {
t.Errorf("got = %v, want %v", got, tt.want)
}
if tt.isChangeDst {
tt.args.dst.A = "some-other-string"
tt.args.dst.B = to.IntPtr(456)
if got := tt.args.src; !reflect.DeepEqual(got, tt.want) {
t.Errorf("src = %v, want %v", got, tt.want)
}
}
})
}
}
1 change: 0 additions & 1 deletion pkg/parser/cse_cmd.sh.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ KUBE_BINARY_URL="{{.KubeBinaryUrl}}"
CUSTOM_KUBE_BINARY_URL="{{.CustomKubeBinaryUrl}}"
KUBEPROXY_URL="{{.KubeproxyUrl}}"

DISABLE_SSH="{{getInverseBoolStr .SshStatus}}"
IS_CUSTOM_CLOUD={{.CustomCloudConfig.IsCustomCloud}}
12 changes: 6 additions & 6 deletions pkg/proto/nbcontract/v1/clusterconfig.pb.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/proto/nbcontract/v1/clusterconfig.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ message ClusterConfig {
string primary_scale_set = 5;

// Cluster network config
ClusterNetworkConfig virtual_network_config = 6;
ClusterNetworkConfig cluster_network_config = 6;

// Specifiy if it uses instance metadata
bool use_instance_metadata = 7; // default to false
Expand Down
Loading
Loading