generated from cybozu-go/neco-template
-
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.
Signed-off-by: Daichi Sakaue <[email protected]>
- Loading branch information
Showing
11 changed files
with
183 additions
and
3 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
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"net/http" | ||
"slices" | ||
"strconv" | ||
|
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,13 @@ | ||
package app | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func init() { | ||
rootCmd.AddCommand(manifestCmd) | ||
} | ||
|
||
var manifestCmd = &cobra.Command{ | ||
Use: "manifest", | ||
Short: "Generate CiliumNetworkPolicy", | ||
Long: `Generate CiliumNetworkPolicy`, | ||
} |
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,106 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var manifestBlastOptions struct { | ||
from string | ||
to string | ||
} | ||
|
||
func init() { | ||
manifestBlastCmd.Flags().StringVar(&manifestBlastOptions.from, "from", "", "egress pod") | ||
manifestBlastCmd.Flags().StringVar(&manifestBlastOptions.to, "to", "", "ingress pod") | ||
manifestCmd.AddCommand(manifestBlastCmd) | ||
} | ||
|
||
var manifestBlastCmd = &cobra.Command{ | ||
Use: "blast", | ||
Short: "Show blast radius of a generated manifest", | ||
Long: `Show blast radius of a generated manifest`, | ||
|
||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runManifestBlast(context.Background(), cmd.OutOrStdout()) | ||
}, | ||
} | ||
|
||
type manifestBlastEntry struct { | ||
Direction string `json:"direction"` | ||
Namespace string `json:"namespace"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func lessManifestBlastEntry(x, y *manifestBlastEntry) bool { | ||
ret := strings.Compare(x.Direction, y.Direction) | ||
if ret == 0 { | ||
ret = strings.Compare(x.Namespace, y.Namespace) | ||
} | ||
if ret == 0 { | ||
ret = strings.Compare(x.Name, y.Name) | ||
} | ||
return ret < 0 | ||
} | ||
|
||
func runManifestBlast(ctx context.Context, w io.Writer) error { | ||
if manifestBlastOptions.from == "" || manifestBlastOptions.to == "" { | ||
return errors.New("--from and --to options are required") | ||
} | ||
|
||
fromSlice := strings.Split(manifestBlastOptions.from, "/") | ||
toSlice := strings.Split(manifestBlastOptions.to, "/") | ||
if len(fromSlice) != 2 || len(toSlice) != 2 { | ||
return errors.New("--from and --to should be NAMESPACE/POD") | ||
} | ||
|
||
_, dynamicClient, err := createK8sClients() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fromIdentity, err := getPodIdentity(ctx, dynamicClient, fromSlice[0], fromSlice[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
toIdentity, err := getPodIdentity(ctx, dynamicClient, toSlice[0], toSlice[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
idEndpoints, err := getIdentityEndpoints(ctx, dynamicClient) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
arr := make([]manifestBlastEntry, 0) | ||
sort.Slice(arr, func(i, j int) bool { return lessManifestBlastEntry(&arr[i], &arr[j]) }) | ||
|
||
for _, ep := range idEndpoints[int(fromIdentity)] { | ||
entry := manifestBlastEntry{ | ||
Direction: directionEgress, | ||
Namespace: ep.GetNamespace(), | ||
Name: ep.GetName(), | ||
} | ||
arr = append(arr, entry) | ||
} | ||
for _, ep := range idEndpoints[int(toIdentity)] { | ||
entry := manifestBlastEntry{ | ||
Direction: directionIngress, | ||
Namespace: ep.GetNamespace(), | ||
Name: ep.GetName(), | ||
} | ||
arr = append(arr, entry) | ||
} | ||
return writeSimpleOrJson(w, arr, []string{"DIRECTION", "NAMESPACE", "NAME"}, len(arr), func(index int) []any { | ||
ep := arr[index] | ||
return []any{ep.Direction, ep.Namespace, ep.Name} | ||
}) | ||
} |
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,30 @@ | ||
package e2e | ||
|
||
import ( | ||
"strings" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testManifestGenerate() { | ||
|
||
} | ||
|
||
func testManifestBlast() { | ||
expected := `Egress,test,self | ||
Ingress,test,l3-ingress-explicit-allow-all | ||
Ingress,test,l3-ingress-explicit-allow-all` | ||
|
||
It("should show blast radius", func() { | ||
from := "--from=test/" + onePodByLabelSelector(Default, "test", "test=self") | ||
to := "--to=test/" + onePodByLabelSelector(Default, "test", "test=l3-ingress-explicit-allow-all") | ||
result := runViewerSafe(Default, nil, "manifest", "blast", from, to, "-o=json") | ||
// remove hash suffix from pod names | ||
result = jqSafe(Default, result, "-r", `[.[] | .name = (.name | split("-") | .[0:5] | join("-"))]`) | ||
result = jqSafe(Default, result, "-r", `[.[] | .name = (.name | if startswith("self") then "self" else . end)]`) | ||
result = jqSafe(Default, result, "-r", `.[] | [.direction, .namespace, .name] | @csv`) | ||
resultString := strings.Replace(string(result), `"`, "", -1) | ||
Expect(resultString).To(Equal(expected), "compare failed.\nactual: %s\nexpected: %s", resultString, expected) | ||
}) | ||
} |
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
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