-
Notifications
You must be signed in to change notification settings - Fork 96
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
base: main
Are you sure you want to change the base?
kmeshctl install #978
Changes from all commits
dba5db3
006932e
97e282a
1cf4744
9539d57
f557c4b
07995b6
661ff07
1e39cec
2438862
5119f9d
ab40442
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still have issue #949. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no ApplyYamlContents in kmesh's kube pkg. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
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.
Add copyright