-
Notifications
You must be signed in to change notification settings - Fork 311
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
Add stack endpoints #1866
Merged
Merged
Add stack endpoints #1866
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c5dbde
Add stack endpoints
jLopezbarb 15b375a
Fix infinite loop
jLopezbarb 2bcb18d
Fix spinner displayed before endpoint list
jLopezbarb 8088589
Merge branch 'main' into jlopezbarb/add-stack-endpoints
jLopezbarb f7f78aa
Fix stacks endpoints order
jLopezbarb ccfb664
Refactor available endpoints message
jLopezbarb File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2021 The Okteto 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 stack | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
contextCMD "github.com/okteto/okteto/cmd/context" | ||
"github.com/okteto/okteto/pkg/cmd/stack" | ||
"github.com/okteto/okteto/pkg/errors" | ||
"github.com/okteto/okteto/pkg/log" | ||
"github.com/okteto/okteto/pkg/okteto" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Endpoints show all the endpoints of a stack | ||
func Endpoints(ctx context.Context) *cobra.Command { | ||
var ( | ||
output string | ||
name string | ||
namespace string | ||
stackPath []string | ||
) | ||
cmd := &cobra.Command{ | ||
Use: "endpoints [service...]", | ||
Short: "Show endpoints for a stack", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
s, err := contextCMD.LoadStackWithContext(ctx, name, namespace, stackPath) | ||
if err != nil { | ||
return err | ||
} | ||
if !okteto.IsOkteto() { | ||
return errors.ErrContextIsNotOktetoCluster | ||
} | ||
|
||
if err := validateOutput(output); err != nil { | ||
return err | ||
} | ||
|
||
if err := stack.ListEndpoints(ctx, s, output); err != nil { | ||
log.Success("Stack '%s' successfully deployed", s.Name) | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVarP(&output, "output", "o", "", "output format. One of: ['json']") | ||
cmd.Flags().StringArrayVarP(&stackPath, "file", "f", []string{}, "path to the stack manifest files. If more than one is passed the latest will overwrite the fields from the previous") | ||
cmd.Flags().StringVarP(&name, "name", "", "", "overwrites the stack name") | ||
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "overwrites the stack namespace where the stack is deployed") | ||
|
||
return cmd | ||
} | ||
|
||
func validateOutput(output string) error { | ||
if output != "" && output != "json" { | ||
return fmt.Errorf("output format is not accepted. Value must be one of: ['json']") | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2021 The Okteto 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 stack | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/okteto/okteto/pkg/log" | ||
"github.com/okteto/okteto/pkg/model" | ||
"github.com/okteto/okteto/pkg/okteto" | ||
) | ||
|
||
func ListEndpoints(ctx context.Context, stack *model.Stack, output string) error { | ||
oktetoClient, err := okteto.NewOktetoClient() | ||
if err != nil { | ||
return err | ||
} | ||
endpointList, err := oktetoClient.ListStackEndpoints(ctx, stack) | ||
if err != nil { | ||
return fmt.Errorf("failed to get preview environments: %s", err) | ||
} | ||
|
||
switch output { | ||
case "json": | ||
bytes, err := json.MarshalIndent(endpointList, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(bytes)) | ||
default: | ||
if len(endpointList) == 0 { | ||
log.Information("There are no available endpoints for stack '%s'\n", stack.Name) | ||
} else { | ||
endpoints := make([]string, 0) | ||
for _, endpoint := range endpointList { | ||
endpoints = append(endpoints, endpoint.URL) | ||
} | ||
sort.Slice(endpoints, func(i, j int) bool { | ||
return len(endpoints[i]) < len(endpoints[j]) | ||
}) | ||
log.Information("Endpoints available:\n - %s\n", strings.Join(endpoints, "\n - ")) | ||
} | ||
} | ||
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,88 @@ | ||
// Copyright 2020 The Okteto 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 okteto | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/okteto/okteto/pkg/model" | ||
"github.com/shurcooL/graphql" | ||
) | ||
|
||
func (c *OktetoClient) ListStackEndpoints(ctx context.Context, stack *model.Stack) ([]Endpoint, error) { | ||
var query struct { | ||
Space struct { | ||
Stacks []struct { | ||
Id graphql.String | ||
Name graphql.String | ||
} | ||
Deployments []struct { | ||
DeployedBy graphql.String | ||
Endpoints []struct { | ||
Url graphql.String | ||
} | ||
} | ||
Statefulsets []struct { | ||
DeployedBy graphql.String | ||
Endpoints []struct { | ||
Url graphql.String | ||
} | ||
} | ||
} `graphql:"space(id: $id)"` | ||
} | ||
|
||
variables := map[string]interface{}{ | ||
"id": graphql.String(stack.Namespace), | ||
} | ||
endpoints := make([]Endpoint, 0) | ||
|
||
err := c.client.Query(ctx, &query, variables) | ||
if err != nil { | ||
return nil, translateAPIErr(err) | ||
} | ||
|
||
var stackId string | ||
for _, queriedStack := range query.Space.Stacks { | ||
if stack.Name == string(queriedStack.Name) { | ||
stackId = string(queriedStack.Id) | ||
} | ||
} | ||
if stackId == "" { | ||
return nil, fmt.Errorf("stack '%s' not found", stack.Name) | ||
} | ||
|
||
for _, d := range query.Space.Deployments { | ||
if string(d.DeployedBy) != stackId { | ||
continue | ||
} | ||
for _, endpoint := range d.Endpoints { | ||
endpoints = append(endpoints, Endpoint{ | ||
URL: string(endpoint.Url), | ||
}) | ||
} | ||
} | ||
|
||
for _, sfs := range query.Space.Statefulsets { | ||
if string(sfs.DeployedBy) != stackId { | ||
continue | ||
} | ||
for _, endpoint := range sfs.Endpoints { | ||
endpoints = append(endpoints, Endpoint{ | ||
URL: string(endpoint.Url), | ||
}) | ||
} | ||
} | ||
return endpoints, nil | ||
} |
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.
@jLopezbarb this is wrong. We could list the ingresses instead and get the URL from there. That wway endpoints are available in vanilla kubernetes clusters too