-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
3,496 additions
and
19 deletions.
There are no files selected for viewing
3 changes: 1 addition & 2 deletions
3
pkg/api/wrappers/wrappersfakes/fake_install_strategy_deployment_interface.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package envvar | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install" | ||
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister" | ||
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil" | ||
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/proxy" | ||
"github.com/sirupsen/logrus" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// NewDeploymentInitializer returns a function that accepts a Deployment object | ||
// and initializes it with env variables specified in operator configuration. | ||
func NewDeploymentInitializer(logger *logrus.Logger, querier proxy.Querier, lister operatorlister.OperatorLister) *DeploymentInitializer { | ||
return &DeploymentInitializer{ | ||
logger: logger, | ||
querier: querier, | ||
config: &operatorConfig{ | ||
lister: lister, | ||
}, | ||
} | ||
} | ||
|
||
type DeploymentInitializer struct { | ||
logger *logrus.Logger | ||
querier proxy.Querier | ||
config *operatorConfig | ||
} | ||
|
||
func (d *DeploymentInitializer) GetDeploymentInitializer(ownerCSV ownerutil.Owner) install.ApplyFunc { | ||
return func(spec *appsv1.Deployment) error { | ||
err := d.initialize(ownerCSV, spec) | ||
return err | ||
} | ||
} | ||
|
||
// Initialize initializes a deployment object with appropriate global cluster | ||
// level proxy env variable(s). | ||
func (d *DeploymentInitializer) initialize(ownerCSV ownerutil.Owner, deployment *appsv1.Deployment) error { | ||
var podConfigEnvVar, proxyEnvVar, merged []corev1.EnvVar | ||
var err error | ||
|
||
podConfigEnvVar, err = d.config.GetOperatorConfig(ownerCSV) | ||
if err != nil { | ||
err = fmt.Errorf("failed to get subscription pod configuration - %v", err) | ||
return err | ||
} | ||
|
||
if !proxy.IsOverridden(podConfigEnvVar) { | ||
proxyEnvVar, err = d.querier.QueryProxyConfig() | ||
if err != nil { | ||
err = fmt.Errorf("failed to query cluster proxy configuration - %v", err) | ||
return err | ||
} | ||
} | ||
|
||
merged = append(podConfigEnvVar, proxyEnvVar...) | ||
|
||
podSpec := deployment.Spec.Template.Spec | ||
if err := InjectEnvIntoDeployment(&podSpec, merged); err != nil { | ||
return fmt.Errorf("failed to inject proxy env variable(s) into deployment spec name=%s - %v", deployment.Name, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package envvar | ||
|
||
import ( | ||
"errors" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// InjectEnvIntoDeployment injects the proxy env variables specified in | ||
// proxyEnvVar into the container(s) of the given PodSpec. | ||
// | ||
// If any Container in PodSpec already defines an env variable of the same name | ||
// as any of the proxy env variables then it | ||
func InjectEnvIntoDeployment(podSpec *corev1.PodSpec, envVars []corev1.EnvVar) error { | ||
if podSpec == nil { | ||
return errors.New("no pod spec provided") | ||
} | ||
|
||
for i := range podSpec.Containers { | ||
container := &podSpec.Containers[i] | ||
container.Env = merge(container.Env, envVars) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func merge(containerEnvVars []corev1.EnvVar, newEnvVars []corev1.EnvVar) (merged []corev1.EnvVar) { | ||
merged = containerEnvVars | ||
|
||
for _, newEnvVar := range newEnvVars { | ||
existing, found := find(containerEnvVars, newEnvVar.Name) | ||
if !found { | ||
if newEnvVar.Value != "" { | ||
merged = append(merged, corev1.EnvVar{ | ||
Name: newEnvVar.Name, | ||
Value: newEnvVar.Value, | ||
}) | ||
} | ||
|
||
continue | ||
} | ||
|
||
existing.Value = newEnvVar.Value | ||
} | ||
|
||
return | ||
} | ||
|
||
func find(proxyEnvVar []corev1.EnvVar, name string) (envVar *corev1.EnvVar, found bool) { | ||
for i := range proxyEnvVar { | ||
if name == proxyEnvVar[i].Name { | ||
// Environment variable names are case sensitive. | ||
found = true | ||
envVar = &proxyEnvVar[i] | ||
|
||
break | ||
} | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.