Skip to content
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

kmeshctl install #978

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions ctl/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"kmesh.net/kmesh/ctl/secret"
"kmesh.net/kmesh/ctl/version"
"kmesh.net/kmesh/ctl/waypoint"
"kmesh.net/kmesh/ctl/install"
)

func GetRootCommand() *cobra.Command {
Expand All @@ -45,6 +46,7 @@ func GetRootCommand() *cobra.Command {
rootCmd.AddCommand(monitoring.NewCmd())
rootCmd.AddCommand(authz.NewCmd())
rootCmd.AddCommand(secret.NewCmd())
rootCmd.AddCommand(install.NewCmd())

return rootCmd
}
123 changes: 123 additions & 0 deletions ctl/install/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright The Kmesh Authors.
*
* 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 install
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add copyright


import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"

"kmesh.net/kmesh/ctl/utils"
)

type GitHubFile struct {
Name string `json:"name"`
DownloadURL string `json:"download_url"`
}

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Short: "install Kmesh with all the resources",
Example: `# Install all Kmesh resources (defaults to main):
kmeshctl install

# Install a specific version of Kmesh
kmeshctl install --version 0.5.0`,
Run: func(cmd *cobra.Command, args []string) {
version, err := cmd.Flags().GetString("version")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have issue #949.
@YaoZengzeng Has this problem been fixed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I hope this issue could be fixed with the solution of #892, but it seems that this problem is not being addressed now. If no one has dealt it by then, I will fix it.

if err != nil {
log.Fatal(err)
}

cli, err := utils.CreateKubeClient()
if err != nil {
log.Fatalf("failed to create cli client: %v", err)
os.Exit(1)
}

fmt.Println("install kmesh version: ", version)
combinedYAMLFile := getYAMLFile(version)

if version != "main" {
versionYaml := strings.Replace(combinedYAMLFile, ":latest", ":"+version, -1)

err = cli.ApplyYAMLContents("", versionYaml)
if err != nil {
log.Fatal(err)
}
} else {
err = cli.ApplyYAMLContents("", combinedYAMLFile)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no ApplyYamlContents in kmesh's kube pkg.
After fixing this problem, this PR can be merged.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, which is why I asked if I should use the istio's implementation or the previous doServerSideApply function which I used at the beginning.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can implement applyYamlContents in Kmesh. If that doesn't work, we can switch back to the doServerSideApply function.

if err != nil {
log.Fatal(err)
}
}
},
}

cmd.Flags().String("version", "main", "Version of the resources to initialize")
return cmd
}

func getYAMLFile(version string) string {
var url string
if version != "main" {
url = fmt.Sprintf("https://api.github.com/repos/kmesh-net/kmesh/contents/deploy/yaml?ref=v%s", version)
} else {
url = "https://api.github.com/repos/kmesh-net/kmesh/contents/deploy/yaml?ref=main"
}

resp, err := http.Get(url)
if err != nil {
log.Fatal("error fetching files: ", err)
}
defer resp.Body.Close()

var files []GitHubFile
err = json.NewDecoder(resp.Body).Decode(&files)
if err != nil {
log.Fatal("error decoding JSON:", err)
}

var combinedYAML string

for _, file := range files {
if filepath.Ext(file.Name) == ".yaml" {
resp, err := http.Get(file.DownloadURL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not use defer in a for loop


deployYaml, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("yamlFile.Get err: %v ", err)
}

combinedYAML += string(deployYaml) + "\n---\n"
}
}

return combinedYAML
}
9 changes: 9 additions & 0 deletions pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/rest"
"k8s.io/utils/ptr"
gatewayapiclient "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
istio "istio.io/istio/pkg/kube"
)

const (
Expand Down Expand Up @@ -57,6 +58,8 @@ type CLIClient interface {
// NewPortForwarder creates a new PortForwarder configured for the given pod. If localPort=0, a port will be
// dynamically selected. If localAddress is empty, "localhost" is used.
NewPortForwarder(podName string, ns string, localAddress string, localPort int, podPort int) (PortForwarder, error)

ApplyYAMLContents(namespace string, yamls ...string) error
}

func NewCLIClient(opts ...ClientOption) (CLIClient, error) {
Expand All @@ -71,6 +74,12 @@ type client struct {
kube kubernetes.Interface
gatewayapi gatewayapiclient.Interface
clientFactory *genericclioptions.ConfigFlags
istioClient istio.CLIClient
}

func (c *client) ApplyYAMLContents(namespace string, yamls ...string) error {
c.istioClient, _ = istio.NewCLIClient(istio.NewClientConfigForRestConfig(c.config))
return c.istioClient.ApplyYAMLContents(namespace, yamls...)
}

func (c *client) NewPortForwarder(podName string, ns string, localAddress string, localPort int, podPort int) (PortForwarder, error) {
Expand Down
Loading