Skip to content

Commit

Permalink
feat: support environment in ExtensionServicesConfig
Browse files Browse the repository at this point in the history
Support setting extension services environment variables in
`ExtensionServicesConfig` document.

Fixes: #8271

Signed-off-by: Noel Georgi <[email protected]>
  • Loading branch information
frezbo committed Feb 6, 2024
1 parent 1e6c8c4 commit 5b1bbec
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 142 deletions.
3 changes: 2 additions & 1 deletion api/resource/definitions/runtime/runtime.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ message ExtensionServicesConfigFile {

// ExtensionServicesConfigSpec describes status of rendered extensions service config files.
message ExtensionServicesConfigSpec {
repeated ExtensionServicesConfigFile files = 2;
repeated ExtensionServicesConfigFile files = 1;
repeated string environment = 2;
}

// ExtensionServicesConfigStatusSpec describes status of rendered extensions service config files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (ctrl *ExtensionServicesConfigController) Run(ctx context.Context, r contro
}
})

logger.Info("extension services config", zap.String("name", ext.Name()), zap.Any("environment", ext.Environment()))
spec.TypedSpec().Environment = ext.Environment()

return nil
}); err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (suite *ExtensionServicesConfigSuite) TestReconcileExtensionServicesConfig(
ExtensionMountPath: "/var/etc/foo",
},
},
ExtensionServiceEnvironment: []string{"FOO=BAR"},
},
}

Expand All @@ -72,6 +73,7 @@ func (suite *ExtensionServicesConfigSuite) TestReconcileExtensionServicesConfig(
content string
mountPath string
}
environment []string
}{
{
extensionName: "test-extension-a",
Expand Down Expand Up @@ -100,6 +102,9 @@ func (suite *ExtensionServicesConfigSuite) TestReconcileExtensionServicesConfig(
mountPath: "/var/etc/foo",
},
},
environment: []string{
"FOO=BAR",
},
},
} {
ctest.AssertResource(suite, tt.extensionName, func(config *runtimeres.ExtensionServicesConfig, asrt *assert.Assertions) {
Expand All @@ -117,6 +122,7 @@ func (suite *ExtensionServicesConfigSuite) TestReconcileExtensionServicesConfig(
})

suite.Assert().Equal(configFileData, spec.Files)
suite.Assert().Equal(tt.environment, spec.Environment)
})
}

Expand Down
12 changes: 7 additions & 5 deletions internal/app/machined/pkg/system/services/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ func (svc *Extension) Runner(r runtime.Runtime) (runner.Runner, error) {

mounts := append([]specs.Mount{}, svc.Spec.Container.Mounts...)

envVars, err := svc.parseEnvironment()
if err != nil {
return nil, err
}

configSpec, err := safe.StateGetByID[*runtimeres.ExtensionServicesConfig](context.Background(), r.State().V1Alpha2().Resources(), svc.Spec.Name)
if err == nil {
spec := configSpec.TypedSpec()
Expand All @@ -178,6 +183,8 @@ func (svc *Extension) Runner(r runtime.Runtime) (runner.Runner, error) {
Options: []string{"ro", "bind"},
})
}

envVars = append(envVars, spec.Environment...)
} else if !state.IsNotFoundError(err) {
return nil, err
}
Expand All @@ -193,11 +200,6 @@ func (svc *Extension) Runner(r runtime.Runtime) (runner.Runner, error) {
restartType = restart.UntilSuccess
}

envVars, err := svc.parseEnvironment()
if err != nil {
return nil, err
}

ociSpecOpts := svc.getOCIOptions(envVars, mounts)

debug := false
Expand Down
252 changes: 131 additions & 121 deletions pkg/machinery/api/resource/definitions/runtime/runtime.pb.go

Large diffs are not rendered by default.

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

1 change: 1 addition & 0 deletions pkg/machinery/config/config/extensions_service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ExtensionServicesConfigConfig interface {
type ExtensionServicesConfig interface {
Name() string
ConfigFiles() []ExtensionServicesConfigFile
Environment() []string
}

// ExtensionServicesConfigFile is a config file for extension services.
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type V1Alpha1 struct {
type ExtensionServiceConfig struct {
ExtensionName string `yaml:"name"`
ExtensionServiceConfigFiles []ExtensionServiceConfigFile `yaml:"configFiles"`
ExtensionServiceEnvironment []string `yaml:"environment"`
}

// ExtensionServiceConfigFile is a config file for extension services.
Expand Down Expand Up @@ -83,8 +84,14 @@ func (e *V1Alpha1) Validate(validation.RuntimeMode, ...validation.Option) ([]str
return nil, fmt.Errorf("extension name is required")
}

if len(ext.ExtensionServiceConfigFiles) == 0 {
return nil, fmt.Errorf("no config files found for extension %q", ext.ExtensionName)
if len(ext.ExtensionServiceConfigFiles) == 0 && len(ext.ExtensionServiceEnvironment) == 0 {
if len(ext.ExtensionServiceConfigFiles) == 0 {
return nil, fmt.Errorf("no config files found for extension %q", ext.ExtensionName)
}

if len(ext.ExtensionServiceEnvironment) == 0 {
return nil, fmt.Errorf("no environment defined for extension %q", ext.ExtensionName)
}
}

for _, file := range ext.ExtensionServiceConfigFiles {
Expand All @@ -101,12 +108,7 @@ func (e *V1Alpha1) Validate(validation.RuntimeMode, ...validation.Option) ([]str
return nil, nil
}

// ExtensionsCfg implements config.ExtensionsConfig interface.
func (e *V1Alpha1) ExtensionsCfg() config.ExtensionServicesConfigConfig {
return e
}

// ConfigData implements config.ExtensionConfig interface.
// ConfigData implements config.ExtensionServicesConfigConfig interface.
func (e *V1Alpha1) ConfigData() []config.ExtensionServicesConfig {
return xslices.Map(e.Config, func(c ExtensionServiceConfig) config.ExtensionServicesConfig {
return &ExtensionServiceConfig{
Expand All @@ -116,12 +118,12 @@ func (e *V1Alpha1) ConfigData() []config.ExtensionServicesConfig {
})
}

// Name implements config.ExtensionConfig interface.
// Name implements config.ExtensionServicesConfig interface.
func (e *ExtensionServiceConfig) Name() string {
return e.ExtensionName
}

// ConfigFiles implements config.ExtensionConfig interface.
// ConfigFiles implements config.ExtensionServicesConfig interface.
func (e *ExtensionServiceConfig) ConfigFiles() []config.ExtensionServicesConfigFile {
return xslices.Map(e.ExtensionServiceConfigFiles, func(c ExtensionServiceConfigFile) config.ExtensionServicesConfigFile {
return &ExtensionServiceConfigFile{
Expand All @@ -131,12 +133,17 @@ func (e *ExtensionServiceConfig) ConfigFiles() []config.ExtensionServicesConfigF
})
}

// Content implements config.ConfigFile interface.
// Environment implements config.ExtensionServicesConfig interface.
func (e *ExtensionServiceConfig) Environment() []string {
return e.ExtensionServiceEnvironment
}

// Content implements config.ExtensionServicesConfigFile interface.
func (e *ExtensionServiceConfigFile) Content() string {
return e.ExtensionContent
}

// Path implements config.ConfigFile interface.
// Path implements config.ExtensionServicesConfigFile interface.
func (e *ExtensionServiceConfigFile) Path() string {
return e.ExtensionMountPath
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestExtensionServicesConfigMarshalStability(t *testing.T) {
ExtensionMountPath: "/etc/foo",
},
},
ExtensionServiceEnvironment: []string{"FOO=BAR"},
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ config:
configFiles:
- content: hello
mountPath: /etc/foo
environment:
- FOO=BAR
4 changes: 4 additions & 0 deletions pkg/machinery/resources/runtime/deep_copy.generated.go

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

3 changes: 2 additions & 1 deletion pkg/machinery/resources/runtime/extension_services_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type ExtensionServicesConfig = typed.Resource[ExtensionServicesConfigSpec, Exten
//
//gotagsrewrite:gen
type ExtensionServicesConfigSpec struct {
Files []ExtensionServicesConfigFile `yaml:"files" protobuf:"2"`
Files []ExtensionServicesConfigFile `yaml:"files" protobuf:"1"`
Environment []string `yaml:"environment" protobuf:"2"`
}

// ExtensionServicesConfigFile describes extensions service config files.
Expand Down

0 comments on commit 5b1bbec

Please sign in to comment.