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
18 changed files
with
613 additions
and
96 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
var dumpOptions struct { | ||
namespace string | ||
} | ||
|
||
func init() { | ||
dumpCmd.Flags().StringVarP(&dumpOptions.namespace, "namespace", "n", "", "namespace of a pod") | ||
rootCmd.AddCommand(dumpCmd) | ||
} | ||
|
||
var dumpCmd = &cobra.Command{ | ||
Use: "dump", | ||
Short: "dump endpoint status", | ||
Long: `Dump endpoint status`, | ||
|
||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runDump(context.Background(), args[0]) | ||
}, | ||
} | ||
|
||
func runDump(ctx context.Context, name string) error { | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientset, _ := kubernetes.NewForConfig(config) | ||
pod, err := clientset.CoreV1().Pods(dumpOptions.namespace).Get(ctx, name, metav1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
node := pod.Spec.NodeName | ||
proxy, err := clientset.CoreV1().Pods("kube-system").List(ctx, metav1.ListOptions{ | ||
FieldSelector: "spec.nodeName=" + node, | ||
LabelSelector: "app.kubernetes.io/name=cilium-agent-proxy", | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
if len(proxy.Items) != 1 { | ||
return errors.New("proxy not found") | ||
} | ||
proxyIP := proxy.Items[0].Status.PodIP | ||
|
||
client, err := dynamic.NewForConfig(config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gvr := schema.GroupVersionResource{ | ||
Group: "cilium.io", | ||
Version: "v2", | ||
Resource: "ciliumendpoints", | ||
} | ||
obj, err := client.Resource(gvr).Namespace(dumpOptions.namespace).Get(ctx, name, metav1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
endpointID, found, err := unstructured.NestedInt64(obj.Object, "status", "id") | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
return errors.New("endpoint not found") | ||
} | ||
|
||
url := fmt.Sprintf("http://%s:8080/v1/endpoint/%d", proxyIP, endpointID) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
data, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(data)) | ||
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,17 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
CILIUM_VERSION := 1.15.3 | ||
|
||
BIN_DIR := $(shell pwd)/../bin | ||
TOOLS_DIR := $(BIN_DIR)/download | ||
CILIUM_POLICY := $(BIN_DIR)/cilium-policy | ||
HELM := $(TOOLS_DIR)/helm | ||
KIND := $(TOOLS_DIR)/kind | ||
KUBECTL := $(TOOLS_DIR)/kubectl | ||
KUSTOMIZE := $(TOOLS_DIR)/kustomize | ||
|
||
##@ Basic | ||
|
||
.PHONY: help | ||
help: ## Display this help | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
##@ Test | ||
|
||
.PHONY: start | ||
start: | ||
docker pull quay.io/cilium/cilium:v$(CILIUM_VERSION) | ||
$(KIND) create cluster --config cluster.yaml | ||
$(KIND) load docker-image quay.io/cilium/cilium:v$(CILIUM_VERSION) | ||
$(HELM) install cilium cilium/cilium --version $(CILIUM_VERSION) \ | ||
--namespace kube-system \ | ||
--set image.pullPolicy=IfNotPresent \ | ||
--set ipam.mode=kubernetes | ||
$(KUSTOMIZE) build . | $(KUBECTL) apply -f - | ||
$(KUBECTL) wait --for=condition=Available --all deployments --all-namespaces --timeout=1h | ||
$(KUBECTL) wait --for=condition=Ready --all pods --all-namespaces --timeout=1h | ||
|
||
.PHONY: install | ||
install: | ||
$(MAKE) -C ../ build | ||
PODNAME=$$($(KUBECTL) get po -l app=ubuntu -o name | cut -d'/' -f2); \ | ||
$(KUBECTL) cp $(CILIUM_POLICY) $${PODNAME}:/tmp/; \ | ||
$(KUBECTL) exec $${PODNAME} -- chmod +x /tmp/cilium-policy | ||
|
||
.PHONY: stop | ||
stop: | ||
$(KIND) delete cluster |
Oops, something went wrong.