Add functions for default api objects

This allows to avoid nil pointers when working with unmarshaled
objects, that may be missing some fields.

Next step would be to revisit why pointers are used in such obj
as Clusterctl and ImageConfiguration

Change-Id: I99a711529a53943ef1b0cb1ebb4d240f2482fac8
This commit is contained in:
Kostiantyn Kalynovskyi 2020-09-22 14:40:46 -05:00
parent eb40a5700f
commit 065888d6c7
7 changed files with 39 additions and 4 deletions

View File

@ -36,3 +36,10 @@ type Cluster struct {
// expecting it to be in document bundle. Parent kubeconfig will be used to get kubeconfig
DynamicKubeConfig bool `json:"dynamicKubeConf,omitempty"`
}
// DefaultClusterMap can be used to safely unmarshal ClusterMap object without nil pointers
func DefaultClusterMap() *ClusterMap {
return &ClusterMap{
Map: make(map[string]*Cluster),
}
}

View File

@ -105,3 +105,12 @@ type MoveOptions struct {
// The namespace where the workload cluster is hosted. If unspecified, the target context's namespace is used.
Namespace string `json:"namespace,omitempty"`
}
// DefaultClusterctl can be used to safely unmarshal Clusterctl object without nil pointers
func DefaultClusterctl() *Clusterctl {
return &Clusterctl{
InitOptions: &InitOptions{},
MoveOptions: &MoveOptions{},
Providers: make([]*Provider, 0),
}
}

View File

@ -49,3 +49,11 @@ type ImageConfiguration struct {
Container *Container `json:"container,omitempty"`
Builder *Builder `json:"builder,omitempty"`
}
// DefaultImageConfiguration can be used to safely unmarshal ImageConfiguration object without nil pointers
func DefaultImageConfiguration() *ImageConfiguration {
return &ImageConfiguration{
Container: &Container{},
Builder: &Builder{},
}
}

View File

@ -34,3 +34,12 @@ type PhaseConfig struct {
ExecutorRef *corev1.ObjectReference `json:"executorRef"`
DocumentEntryPoint string `json:"documentEntryPoint"`
}
// DefaultPhase can be used to safely unmarshal phase object without nil pointers
func DefaultPhase() *Phase {
return &Phase{
Config: PhaseConfig{
ExecutorRef: &corev1.ObjectReference{},
},
}
}

View File

@ -42,7 +42,7 @@ type Executor struct {
// RegisterExecutor adds executor to phase executor registry
func RegisterExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory) error {
obj := &v1alpha1.ImageConfiguration{}
obj := v1alpha1.DefaultImageConfiguration()
gvks, _, err := v1alpha1.Scheme.ObjectKinds(obj)
if err != nil {
return err

View File

@ -55,7 +55,7 @@ func RegisterExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory)
// NewExecutor creates instance of 'clusterctl init' phase executor
func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
options := &airshipv1.Clusterctl{}
options := airshipv1.DefaultClusterctl()
if err := cfg.ExecutorDocument.ToAPIObject(options, airshipv1.Scheme); err != nil {
return nil, err
}

View File

@ -76,6 +76,8 @@ func (helper *Helper) Phase(phaseID ifc.ID) (*v1alpha1.Phase, error) {
if err != nil {
return nil, err
}
// Overwrite phase used for selector, with a phase with default values
phase = v1alpha1.DefaultPhase()
if err = doc.ToAPIObject(phase, v1alpha1.Scheme); err != nil {
return nil, err
}
@ -126,7 +128,7 @@ func (helper *Helper) ListPhases() ([]*v1alpha1.Phase, error) {
phases := []*v1alpha1.Phase{}
for _, doc := range docs {
p := &v1alpha1.Phase{}
p := v1alpha1.DefaultPhase()
if err = doc.ToAPIObject(p, v1alpha1.Scheme); err != nil {
return nil, err
}
@ -142,7 +144,7 @@ func (helper *Helper) ClusterMapAPIobj() (*v1alpha1.ClusterMap, error) {
return nil, err
}
cMap := &v1alpha1.ClusterMap{}
cMap := v1alpha1.DefaultClusterMap()
selector, err := document.NewSelector().ByObject(cMap, v1alpha1.Scheme)
if err != nil {
return nil, err