Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
New command: skuba addon refresh localconfig (bsc#1173055) (#1226)
Browse files Browse the repository at this point in the history
Implements skuba addon refresh localconfig, related bugzilla
. bsc#1173055
. bsc#1172805

Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>
  • Loading branch information
JenTing Hsiao authored Jul 13, 2020
1 parent bb9a473 commit d3c5001
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/skuba/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewAddonCmd() *cobra.Command {
}

cmd.AddCommand(
addons.NewRefreshCmd(),
addons.NewUpgradeCmd(),
)

Expand Down
62 changes: 62 additions & 0 deletions cmd/skuba/addon/refresh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2020 SUSE LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package addons

import (
"fmt"
"os"

"github.com/spf13/cobra"
"k8s.io/klog"

"github.com/SUSE/skuba/internal/pkg/skuba/kubernetes"
addons "github.com/SUSE/skuba/pkg/skuba/actions/addon/refresh"
)

// NewRefreshCmd creates a new `skuba addon refresh` cobra command
func NewRefreshCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "refresh",
Short: "Manages addon refresh operations",
}

cmd.AddCommand(
newRefreshLocalConfigCmd(),
)

return cmd
}

func newRefreshLocalConfigCmd() *cobra.Command {
return &cobra.Command{
Use: "localconfig",
Short: "Update local cluster definition folder configuration",
Run: func(cmd *cobra.Command, args []string) {
clientSet, err := kubernetes.GetAdminClientSet()
if err != nil {
klog.Errorf("unable to get admin client set: %s", err)
os.Exit(1)
}
if err := addons.AddonsBaseManifest(clientSet); err != nil {
fmt.Printf("Unable to update addons base manifests: %s\n", err)
os.Exit(1)
}
},
Args: cobra.NoArgs,
}
}
18 changes: 18 additions & 0 deletions docs/man/skuba-addon-refresh-localconfig.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
% skuba-addon-refresh-localconfig(1) # skuba addon refresh localconfig - Update local addon definition folder configuration

# NAME

localconfig - Update local addon definition folder configuration

# SYNOPSIS
**localconfig**
[**--help**|**-h**]
*localconfig* [-h]

# DESCRIPTION
**localconfig** Update local addon definition folder configuration

# OPTIONS

**--help, -h**
Print usage statement.
1 change: 1 addition & 0 deletions docs/man/skuba.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ reconfiguration in an easy way.
**skuba-node-remove**(1),
**skuba-node-upgrade-plan**(1),
**skuba-node-upgrade-apply**(1),
**skuba-addon-refresh-localconfig**(1),
**skuba-addon-upgrade-plan**(1),
**skuba-addon-upgrade-apply**(1)
13 changes: 13 additions & 0 deletions internal/pkg/skuba/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ func (addon Addon) preflightManifestFilename() string {
return fmt.Sprintf("%s-preflight.yaml", addon.Addon)
}

func (addon Addon) legacyManifestPath(rootDir string) string {
return filepath.Join(rootDir, addon.manifestFilename())
}

func (addon Addon) manifestPath(rootDir string) string {
return filepath.Join(addon.baseResourcesDir(rootDir), addon.manifestFilename())
}
Expand Down Expand Up @@ -318,6 +322,15 @@ func (addon Addon) Write(addonConfiguration AddonConfiguration) error {
if err := os.MkdirAll(patchResourcesDir, 0700); err != nil {
return errors.Wrapf(err, "unable to create directory: %s", patchResourcesDir)
}

// migrates legacy addon manifest if existed
legacyManifestPath := addon.legacyManifestPath(addon.addonDir())
if f, err := os.Stat(legacyManifestPath); !os.IsNotExist(err) && !f.IsDir() {
if err := os.Remove(legacyManifestPath); err != nil {
return errors.Wrapf(err, "unable to remove %s addon legacy rendered template", addon.Addon)
}
}

if err := ioutil.WriteFile(addon.manifestPath(addon.addonDir()), []byte(addonTemplateWarning+addonManifest), 0600); err != nil {
return errors.Wrapf(err, "unable to write %s addon rendered template", addon.Addon)
}
Expand Down
87 changes: 87 additions & 0 deletions internal/pkg/skuba/addons/addons_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2020 SUSE LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package addons

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/SUSE/skuba/internal/pkg/skuba/kubernetes"
skubaconstants "github.com/SUSE/skuba/pkg/skuba"
)

func TestAddonLegacyManifestMigration(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Errorf("unable to get current directory: %v", err)
return
}

defer func() {
// removes rendered addon folder
dir := filepath.Join(pwd, "addons")
if f, err := os.Stat(dir); !os.IsNotExist(err) && f.IsDir() {
if err := os.RemoveAll(dir); err != nil {
t.Errorf("unable to remove rendered addon folder: %v", err)
return
}
}
}()

// create legacy addons manifest folder
if err := os.Mkdir(filepath.Join(pwd, skubaconstants.AddonsDir()), 0700); err != nil {
t.Errorf("unable to create directory %s: %v", skubaconstants.AddonsDir(), err)
return
}
for _, addon := range Addons {
addonDir := filepath.Join(pwd, addon.addonDir())
if err := os.Mkdir(addonDir, 0700); err != nil {
t.Errorf("unable to create directory %s: %v", addonDir, err)
return
}
lagacyManifestPath := addon.legacyManifestPath(addonDir)
if err := ioutil.WriteFile(lagacyManifestPath, []byte(""), 0600); err != nil {
t.Errorf("unable to write legacy addon manifest: %v", err)
return
}
}

addonConfiguration := AddonConfiguration{
ClusterVersion: kubernetes.LatestVersion(),
ControlPlane: "unit.test",
ClusterName: "unit-test",
}
// render new addons manifest folder
for _, addon := range Addons {
if err := addon.Write(addonConfiguration); err != nil {
t.Errorf("expected no error, but got error: %v", err)
return
}
}

// check the legacy addons manifest gone
for _, addon := range Addons {
lagacyManifestPath := addon.legacyManifestPath(filepath.Join(pwd, addon.addonDir()))
if _, err := os.Stat(lagacyManifestPath); !os.IsNotExist(err) {
t.Error("expected legacy manifest not exists")
return
}
}
}
56 changes: 56 additions & 0 deletions pkg/skuba/actions/addon/refresh/localconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2020 SUSE LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package refresh

import (
"fmt"

"github.com/pkg/errors"
clientset "k8s.io/client-go/kubernetes"

"github.com/SUSE/skuba/internal/pkg/skuba/addons"
"github.com/SUSE/skuba/internal/pkg/skuba/kubeadm"
)

// AddonsBaseManifest implements the `skuba addon refresh localconfig` command.
func AddonsBaseManifest(client clientset.Interface) error {
currentClusterVersion, err := kubeadm.GetCurrentClusterVersion(client)
if err != nil {
return err
}

clusterConfiguration, err := kubeadm.GetClusterConfiguration(client)
if err != nil {
return errors.Wrap(err, "Could not fetch cluster configuration")
}

// re-render all addons manifest
addonConfiguration := addons.AddonConfiguration{
ClusterVersion: currentClusterVersion,
ControlPlane: clusterConfiguration.ControlPlaneEndpoint,
ClusterName: clusterConfiguration.ClusterName,
}
for addonName, addon := range addons.Addons {
if err := addon.Write(addonConfiguration); err != nil {
return errors.Wrapf(err, "failed to refresh addon %s manifest", string(addonName))
}
}

fmt.Println("Successfully refreshed addons base manifests")
return nil
}

0 comments on commit d3c5001

Please sign in to comment.