-
Notifications
You must be signed in to change notification settings - Fork 103
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
Refactor operator package installation #1542
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,3 @@ | ||
// Package install provides function to install package resources | ||
// on a Kubernetes cluster. | ||
package install |
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,51 @@ | ||
package install | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
pollwait "k8s.io/apimachinery/pkg/util/wait" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/clog" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo" | ||
) | ||
|
||
func installInstance(client *kudo.Client, instance *v1beta1.Instance) error { | ||
existingInstance, err := client.GetInstance(instance.Name, instance.Namespace) | ||
if err != nil { | ||
return fmt.Errorf("failed to verify existing instance: %v", err) | ||
} | ||
|
||
if existingInstance != nil { | ||
return clog.Errorf( | ||
"cannot install instance '%s' because an instance of that name already exists in namespace %s", | ||
instance.Name, | ||
instance.Namespace) | ||
} | ||
|
||
if _, err := client.InstallInstanceObjToCluster(instance, instance.Namespace); err != nil { | ||
return fmt.Errorf( | ||
"failed to install instance %s/%s: %v", | ||
instance.Namespace, | ||
instance.Name, | ||
err) | ||
} | ||
|
||
clog.Printf("instance %s/%s created", instance.Namespace, instance.Name) | ||
return nil | ||
} | ||
|
||
func waitForInstance(client *kudo.Client, instance *v1beta1.Instance, timeout time.Duration) error { | ||
err := client.WaitForInstance(instance.Name, instance.Namespace, nil, timeout) | ||
if errors.Is(err, pollwait.ErrWaitTimeout) { | ||
clog.Printf("timeout waiting for instance %s/%s", instance.Namespace, instance.Name) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("failed to wait on instance %s/%s: %v", instance.Namespace, instance.Name, err) | ||
} | ||
|
||
clog.Printf("instance %s/%s ready", instance.Namespace, instance.Name) | ||
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 install | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kudobuilder/kudo/pkg/engine/renderer" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/clog" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo" | ||
) | ||
|
||
// installNamespace installs a namespace from package resources. | ||
// If the resources contain a namespace manifest, it is rendered and | ||
// applied to the namespace. | ||
func installNamespace( | ||
client *kudo.Client, | ||
resources packages.Resources, | ||
parameters map[string]string) error { | ||
clog.V(3).Printf("creating namespace: %q", resources.Instance.Namespace) | ||
|
||
manifest := "" | ||
if resources.Operator.Spec.NamespaceManifest != "" { | ||
clog.V(3).Printf( | ||
"creating namespace with manifest named: %q", | ||
resources.Operator.Spec.NamespaceManifest) | ||
|
||
template, ok := resources.OperatorVersion.Spec.Templates[resources.Operator.Spec.NamespaceManifest] | ||
if !ok { | ||
return fmt.Errorf( | ||
"failed to find template for namespace manifest %q", | ||
resources.Operator.Spec.NamespaceManifest) | ||
} | ||
|
||
var err error | ||
manifest, err = renderNamespaceManifest(template, resources, parameters) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"failed to render namespace manifest %q: %w", | ||
resources.Operator.Spec.NamespaceManifest, | ||
err) | ||
} | ||
} | ||
|
||
return client.CreateNamespace(resources.Instance.Namespace, manifest) | ||
} | ||
|
||
func renderNamespaceManifest( | ||
manifest string, | ||
resources packages.Resources, | ||
parameters map[string]string) (string, error) { | ||
configs := renderer.NewVariableMap(). | ||
WithInstance("", resources.Instance.Name, resources.Instance.Namespace, "", ""). | ||
WithResource(&resources). | ||
WithParameterStrings(parameters) | ||
engine := renderer.New() | ||
rendered, err := engine.Render("namespace", manifest, configs) | ||
if err != nil { | ||
return "", err | ||
} | ||
return rendered, 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,39 @@ | ||
package install | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNamespaceManifestRendering(t *testing.T) { | ||
namespaceFile := "testdata/namespace.yaml" | ||
|
||
ns, err := ioutil.ReadFile(namespaceFile) | ||
assert.NoError(t, err) | ||
|
||
params := make(map[string]string) | ||
params["foo"] = "bar-param" | ||
|
||
rendered, err := renderNamespaceManifest(string(ns), testResources(), params) | ||
assert.NoError(t, err) | ||
|
||
file := "rendered-namespace.yaml" | ||
gf := filepath.Join("testdata", file+".golden") | ||
|
||
if *update { | ||
t.Log("update golden file") | ||
if err := ioutil.WriteFile(gf, []byte(rendered), 0644); err != nil { | ||
t.Fatalf("failed to update golden file: %s", err) | ||
} | ||
} | ||
|
||
golden, err := ioutil.ReadFile(gf) | ||
if err != nil { | ||
t.Fatalf("failed reading .golden: %s", err) | ||
} | ||
|
||
assert.Equal(t, string(golden), rendered, "for golden file: %s", gf) | ||
} |
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,59 @@ | ||
package install | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/thoas/go-funk" | ||
|
||
"github.com/kudobuilder/kudo/pkg/kudoctl/clog" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo" | ||
) | ||
|
||
func installOperatorAndOperatorVersion(client *kudo.Client, resources packages.Resources) error { | ||
if !client.OperatorExistsInCluster(resources.Operator.Name, resources.Operator.Namespace) { | ||
if _, err := client.InstallOperatorObjToCluster(resources.Operator, resources.Operator.Namespace); err != nil { | ||
return fmt.Errorf( | ||
"failed to install operator %s/%s: %v", | ||
resources.Operator.Namespace, | ||
resources.Operator.Name, | ||
err) | ||
} | ||
clog.Printf( | ||
"operator %s/%s created", | ||
resources.Operator.Namespace, | ||
resources.Operator.Name) | ||
} | ||
|
||
versionsInstalled, err := client.OperatorVersionsInstalled(resources.Operator.Name, resources.Operator.Namespace) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"failed to retrieve existing operator versions of operator %s/%s: %v", | ||
resources.Operator.Namespace, | ||
resources.Operator.Name, | ||
err) | ||
} | ||
|
||
if !funk.ContainsString(versionsInstalled, resources.OperatorVersion.Spec.Version) { | ||
if _, err := client.InstallOperatorVersionObjToCluster( | ||
resources.OperatorVersion, | ||
resources.OperatorVersion.Namespace); err != nil { | ||
return fmt.Errorf( | ||
"failed to install operatorversion %s/%s: %v", | ||
resources.OperatorVersion.Namespace, | ||
resources.OperatorVersion.Name, | ||
err) | ||
} | ||
clog.Printf( | ||
"operatorversion %s/%s created", | ||
resources.OperatorVersion.Namespace, | ||
resources.OperatorVersion.Name) | ||
} else { | ||
clog.Printf( | ||
"operatorversion %s/%s already installed", | ||
resources.OperatorVersion.Namespace, | ||
resources.OperatorVersion.Name) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, we'll need this method for the dependencies.