Skip to content

Commit

Permalink
platform/conf: support ignition 3.1, 3.2 and 3.3
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Tortuyaux <[email protected]>
  • Loading branch information
tormath1 committed Mar 3, 2022
1 parent 5d1952f commit d55c6c8
Showing 1 changed file with 130 additions and 1 deletion.
131 changes: 130 additions & 1 deletion platform/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ import (
ign3err "github.com/flatcar-linux/ignition/v2/config/shared/errors"
v3 "github.com/flatcar-linux/ignition/v2/config/v3_0"
v3types "github.com/flatcar-linux/ignition/v2/config/v3_0/types"
v31 "github.com/flatcar-linux/ignition/v2/config/v3_1"
v31types "github.com/flatcar-linux/ignition/v2/config/v3_1/types"
v32 "github.com/flatcar-linux/ignition/v2/config/v3_2"
v32types "github.com/flatcar-linux/ignition/v2/config/v3_2/types"
v33 "github.com/flatcar-linux/ignition/v2/config/v3_3"
v33types "github.com/flatcar-linux/ignition/v2/config/v3_3/types"
ign3validate "github.com/flatcar-linux/ignition/v2/config/validate"
"github.com/vincent-petithory/dataurl"
"golang.org/x/crypto/ssh/agent"
Expand Down Expand Up @@ -75,6 +81,9 @@ type Conf struct {
ignitionV22 *v22types.Config
ignitionV23 *v23types.Config
ignitionV3 *v3types.Config
ignitionV31 *v31types.Config
ignitionV32 *v32types.Config
ignitionV33 *v33types.Config
cloudconfig *cci.CloudConfig
script string
}
Expand Down Expand Up @@ -226,6 +235,33 @@ func (u *UserData) Render(ctPlatform string) (*Conf, error) {
return err
}

ignc31, report31, err := v31.Parse([]byte(u.data))
if err == nil {
c.ignitionV31 = &ignc31
return nil
} else if err != ign3err.ErrUnknownVersion {
plog.Errorf("invalid userdata: %v", report31)
return err
}

ignc32, report32, err := v32.Parse([]byte(u.data))
if err == nil {
c.ignitionV32 = &ignc32
return nil
} else if err != ign3err.ErrUnknownVersion {
plog.Errorf("invalid userdata: %v", report32)
return err
}

ignc33, report33, err := v33.Parse([]byte(u.data))
if err == nil {
c.ignitionV33 = &ignc33
return nil
} else if err != ign3err.ErrUnknownVersion {
plog.Errorf("invalid userdata: %v", report33)
return err
}

// give up
return err
}
Expand Down Expand Up @@ -295,6 +331,15 @@ func (c *Conf) String() string {
} else if c.ignitionV3 != nil {
buf, _ := json.Marshal(c.ignitionV3)
return string(buf)
} else if c.ignitionV31 != nil {
buf, _ := json.Marshal(c.ignitionV31)
return string(buf)
} else if c.ignitionV32 != nil {
buf, _ := json.Marshal(c.ignitionV32)
return string(buf)
} else if c.ignitionV33 != nil {
buf, _ := json.Marshal(c.ignitionV33)
return string(buf)
} else if c.cloudconfig != nil {
return c.cloudconfig.String()
} else if c.script != "" {
Expand All @@ -310,6 +355,21 @@ func (c *Conf) MergeV3(newConfig v3types.Config) {
c.ignitionV3 = &mergeConfig
}

func (c *Conf) MergeV31(newConfig v31types.Config) {
mergeConfig := v31.Merge(*c.ignitionV31, newConfig)
c.ignitionV31 = &mergeConfig
}

func (c *Conf) MergeV32(newConfig v32types.Config) {
mergeConfig := v32.Merge(*c.ignitionV32, newConfig)
c.ignitionV32 = &mergeConfig
}

func (c *Conf) MergeV33(newConfig v33types.Config) {
mergeConfig := v33.Merge(*c.ignitionV33, newConfig)
c.ignitionV33 = &mergeConfig
}

func (c *Conf) ValidConfig() bool {
if !c.IsIgnition() {
return false
Expand Down Expand Up @@ -851,6 +911,69 @@ func (c *Conf) copyKeysIgnitionV3(keys []*agent.Key) {
c.MergeV3(newConfig)
}

func (c *Conf) copyKeysIgnitionV31(keys []*agent.Key) {
var keyObjs []v31types.SSHAuthorizedKey
for _, key := range keys {
keyObjs = append(keyObjs, v31types.SSHAuthorizedKey(key.String()))
}
newConfig := v31types.Config{
Ignition: v31types.Ignition{
Version: "3.1.0",
},
Passwd: v31types.Passwd{
Users: []v31types.PasswdUser{
{
Name: "core",
SSHAuthorizedKeys: keyObjs,
},
},
},
}
c.MergeV31(newConfig)
}

func (c *Conf) copyKeysIgnitionV32(keys []*agent.Key) {
var keyObjs []v32types.SSHAuthorizedKey
for _, key := range keys {
keyObjs = append(keyObjs, v32types.SSHAuthorizedKey(key.String()))
}
newConfig := v32types.Config{
Ignition: v32types.Ignition{
Version: "3.2.0",
},
Passwd: v32types.Passwd{
Users: []v32types.PasswdUser{
{
Name: "core",
SSHAuthorizedKeys: keyObjs,
},
},
},
}
c.MergeV32(newConfig)
}

func (c *Conf) copyKeysIgnitionV33(keys []*agent.Key) {
var keyObjs []v33types.SSHAuthorizedKey
for _, key := range keys {
keyObjs = append(keyObjs, v33types.SSHAuthorizedKey(key.String()))
}
newConfig := v33types.Config{
Ignition: v33types.Ignition{
Version: "3.3.0",
},
Passwd: v33types.Passwd{
Users: []v33types.PasswdUser{
{
Name: "core",
SSHAuthorizedKeys: keyObjs,
},
},
},
}
c.MergeV33(newConfig)
}

func (c *Conf) copyKeysCloudConfig(keys []*agent.Key) {
c.cloudconfig.SSHAuthorizedKeys = append(c.cloudconfig.SSHAuthorizedKeys, keysToStrings(keys)...)
}
Expand All @@ -875,6 +998,12 @@ func (c *Conf) CopyKeys(keys []*agent.Key) {
c.copyKeysIgnitionV23(keys)
} else if c.ignitionV3 != nil {
c.copyKeysIgnitionV3(keys)
} else if c.ignitionV31 != nil {
c.copyKeysIgnitionV31(keys)
} else if c.ignitionV32 != nil {
c.copyKeysIgnitionV32(keys)
} else if c.ignitionV33 != nil {
c.copyKeysIgnitionV33(keys)
} else if c.cloudconfig != nil {
c.copyKeysCloudConfig(keys)
} else if c.script != "" {
Expand All @@ -893,7 +1022,7 @@ func keysToStrings(keys []*agent.Key) (keyStrs []string) {
// Returns false in the case of empty configs as on most platforms,
// this will default back to cloudconfig
func (c *Conf) IsIgnition() bool {
return c.ignitionV1 != nil || c.ignitionV2 != nil || c.ignitionV21 != nil || c.ignitionV22 != nil || c.ignitionV23 != nil || c.ignitionV3 != nil
return c.ignitionV1 != nil || c.ignitionV2 != nil || c.ignitionV21 != nil || c.ignitionV22 != nil || c.ignitionV23 != nil || c.ignitionV3 != nil || c.ignitionV31 != nil || c.ignitionV32 != nil || c.ignitionV33 != nil
}

func (c *Conf) IsEmpty() bool {
Expand Down

0 comments on commit d55c6c8

Please sign in to comment.