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
21 changed files
with
925 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Build the manager binary | ||
FROM ghcr.io/cybozu/golang:1.23-jammy AS builder | ||
|
||
# Copy the Go Modules manifests | ||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
# cache deps before building and copying source so that we don't need to re-download as much | ||
# and so that source changes don't invalidate our downloaded layer | ||
RUN go mod download | ||
|
||
# Copy the go source | ||
COPY cmd/cilium-agent-proxy/ cmd/cilium-agent-proxy/ | ||
|
||
# Build | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o cilium-agent-proxy ./cmd/cilium-agent-proxy | ||
|
||
# Compose the manager container | ||
FROM ghcr.io/cybozu/ubuntu:22.04 | ||
LABEL org.opencontainers.image.source=https://github.com/cybozu-go/network-policy-viewer | ||
|
||
WORKDIR / | ||
COPY --from=builder /work/cilium-agent-proxy / | ||
|
||
USER 10000:10000 | ||
ENTRYPOINT ["/cilium-agent-proxy"] |
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,7 @@ | ||
package main | ||
|
||
import "github.com/cybozu-go/network-policy-viewer/cmd/cilium-agent-proxy/sub" | ||
|
||
func main() { | ||
sub.Execute() | ||
} |
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,28 @@ | ||
package sub | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "cilium-agent-proxy", | ||
Short: "cilium-agent proxy", | ||
Long: `cilium-agent proxy`, | ||
|
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
return subMain() | ||
}, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
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,109 @@ | ||
package sub | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
const socketPath = "/var/run/cilium/cilium.sock" | ||
|
||
var ( | ||
endpointPattern *regexp.Regexp | ||
identityPattern *regexp.Regexp | ||
) | ||
|
||
func handleEndpoint(w http.ResponseWriter, r *http.Request) { | ||
match := endpointPattern.FindStringSubmatch(r.URL.Path) | ||
if match == nil || len(match) < 2 { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
client := http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", socketPath) | ||
}, | ||
}, | ||
} | ||
|
||
// Convert to number to avoid possible parameter injection | ||
endpoint, err := strconv.Atoi(match[1]) | ||
if err != nil { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
|
||
url := fmt.Sprintf("http://localhost/v1/endpoint/%d", endpoint) | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
|
||
io.Copy(w, resp.Body) | ||
} | ||
|
||
func handleIdentity(w http.ResponseWriter, r *http.Request) { | ||
match := identityPattern.FindStringSubmatch(r.URL.Path) | ||
if match == nil || len(match) < 2 { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
client := http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", socketPath) | ||
}, | ||
}, | ||
} | ||
|
||
// Convert to number to avoid possible parameter injection | ||
identity, err := strconv.Atoi(match[1]) | ||
if err != nil { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
|
||
url := fmt.Sprintf("http://localhost/v1/identity/%d", identity) | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
fmt.Fprint(w, "error\n") | ||
return | ||
} | ||
|
||
io.Copy(w, resp.Body) | ||
} | ||
|
||
func handlePolicy(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "policy") | ||
} | ||
|
||
func subMain() error { | ||
pattern, err := regexp.Compile(`^/endpoint/(?P<endpoint>\d+)$`) | ||
endpointPattern = pattern | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pattern, err = regexp.Compile(`^/identity/(?P<identity>\d+)$`) | ||
identityPattern = pattern | ||
if err != nil { | ||
return err | ||
} | ||
|
||
server := http.Server{ | ||
Addr: ":8080", | ||
Handler: nil, | ||
} | ||
|
||
http.HandleFunc("/endpoint/", handleEndpoint) | ||
http.HandleFunc("/identity/", handleIdentity) | ||
http.HandleFunc("/policy/", handlePolicy) | ||
|
||
return server.ListenAndServe() | ||
} |
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,13 @@ | ||
package cmd | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var l3Cmd = &cobra.Command{ | ||
Use: "l3", | ||
Short: "inspect l3 rules", | ||
Long: `inspect l3 rules`, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(l3Cmd) | ||
} |
Oops, something went wrong.