forked from openservicemesh/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add mesh check-upgrade command
Resolves openservicemesh#1720. Signed-off-by: Johnson Shi <Johnson.Shi@microsoft.com>
- Loading branch information
1 parent
83c2a38
commit eae0eca
Showing
4 changed files
with
166 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"net/http" | ||
|
||
"github.com/openservicemesh/osm/pkg/constants" | ||
) | ||
|
||
const ( | ||
osmReleaseRepoOwner = "openservicemesh" | ||
osmReleaseRepoName = "osm" | ||
) | ||
|
||
const meshCheckUpgradeDesc = ` | ||
This command checks if there are upgrades available for a given OSM mesh and namespace. | ||
The following information is printed: | ||
(1) the OSM version installed in the mesh name and namespace, | ||
(2) the latest OSM release version available. | ||
The mesh to check for updates is identified by its mesh name and namespace. | ||
If either were overridden from the default for the "osm install" command, | ||
the --mesh-name and --osm-namespace flags need to be specified. | ||
` | ||
|
||
const meshCheckUpgradeExample = ` | ||
# Check if upgrades are available for a mesh and print version information. | ||
osm mesh check-upgrade --mesh-name osm --osm-namespace osm-system | ||
` | ||
|
||
type meshCheckUpgradeCmd struct { | ||
meshName string | ||
out io.Writer | ||
config *rest.Config | ||
clientSet kubernetes.Interface | ||
} | ||
|
||
func newMeshCheckUpgrade(out io.Writer) *cobra.Command { | ||
checkUpgradeCmd := &meshCheckUpgradeCmd{ | ||
out: out, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "check-upgrade", | ||
Short: "check if upgrades are available for an OSM mesh and print version information", | ||
Long: meshCheckUpgradeDesc, | ||
Example: meshCheckUpgradeExample, | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
config, err := settings.RESTClientGetter().ToRESTConfig() | ||
if err != nil { | ||
return errors.Errorf("Error fetching kubeconfig: %s", err) | ||
} | ||
checkUpgradeCmd.config = config | ||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err) | ||
} | ||
checkUpgradeCmd.clientSet = clientset | ||
return checkUpgradeCmd.run() | ||
}, | ||
} | ||
|
||
f := cmd.Flags() | ||
f.StringVar(&checkUpgradeCmd.meshName, "mesh-name", defaultMeshName, "Name of the mesh to check for upgrades") | ||
|
||
return cmd | ||
} | ||
|
||
func (c *meshCheckUpgradeCmd) run() error { | ||
meshNamespace := settings.Namespace() | ||
osmControllerDeployment, err := getMeshControllerDeployment(c.clientSet, c.meshName, meshNamespace) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// get mesh version currently installed | ||
meshVersion := osmControllerDeployment.ObjectMeta.Labels[constants.OSMAppVersionLabelKey] | ||
if meshVersion == "" { | ||
meshVersion = "Unknown" | ||
} | ||
_, _ = fmt.Fprintf(c.out, "Mesh Name: [%s]. Mesh Namespace: [%s]. Mesh Version: [%s].\n", c.meshName, meshNamespace, meshVersion) | ||
|
||
// get latest available release version from the osm repo release API endpoint | ||
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", osmReleaseRepoOwner, osmReleaseRepoName) | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return fmt.Errorf("error fetching latest available release version from %s", url) | ||
} | ||
|
||
req.Header.Add("Accept", "application/vnd.github.v3+json") | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("error fetching latest available release version from %s", url) | ||
} | ||
defer resp.Body.Close() //nolint: errcheck,gosec | ||
|
||
latestReleaseVersionInfo := map[string]interface{}{} | ||
if err := json.NewDecoder(resp.Body).Decode(&latestReleaseVersionInfo); err != nil { | ||
return fmt.Errorf("error json decoding latest release version info from %s", url) | ||
} | ||
|
||
if latestVersion := latestReleaseVersionInfo["tag_name"]; latestVersion != "" { | ||
_, _ = fmt.Fprintf(c.out, "Latest Available Version: [%s].\n", latestVersion) | ||
} else { | ||
return fmt.Errorf("error fetching latest available release version from %s", url) | ||
} | ||
|
||
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
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