diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 6d36ed8..4a752cf 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -44,14 +44,14 @@ jobs: push: true tags: ghcr.io/cybozu-go/login-protector:${{ inputs.tag }},ghcr.io/cybozu-go/login-protector:latest target: login-protector - - name: Build and Push tty-exporter + - name: Build and Push local-session-tracker uses: docker/build-push-action@v6 with: context: . file: ./Dockerfile push: true - tags: ghcr.io/cybozu-go/tty-exporter:${{ inputs.tag }},ghcr.io/cybozu-go/tty-exporter:latest - target: tty-exporter + tags: ghcr.io/cybozu-go/local-session-tracker:${{ inputs.tag }},ghcr.io/cybozu-go/local-session-tracker:latest + target: local-session-tracker - name: Setup Git Config run: | git config --global user.name github-actions diff --git a/Dockerfile b/Dockerfile index 431cb78..997402a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,13 +15,13 @@ COPY internal/ internal/ RUN CGO_ENABLED=0 go install -ldflags="-w -s" ./cmd/... -# Build the tty-exporter binary -FROM scratch AS tty-exporter +# Build the local-session-tracker binary +FROM scratch AS local-session-tracker LABEL org.opencontainers.image.source="https://github.com/cybozu-go/login-protector" -COPY --from=build /go/bin/tty-exporter . +COPY --from=build /go/bin/local-session-tracker . USER 10000:10000 -ENTRYPOINT ["/tty-exporter"] +ENTRYPOINT ["/local-session-tracker"] # Build the login-protector binary FROM scratch AS login-protector diff --git a/Makefile b/Makefile index 8f3d064..05f2cfd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Image URL to use all building/pushing image targets PROTECTOR_IMG ?= login-protector:dev -EXPORTER_IMG ?= tty-exporter:dev +TRACKER_IMG ?= local-session-tracker:dev # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) @@ -83,17 +83,17 @@ run: manifests fmt vet ## Run a controller from your host. .PHONY: docker-build docker-build: ## Build docker image with the manager. $(CONTAINER_TOOL) build -t ${PROTECTOR_IMG} . --target=login-protector - $(CONTAINER_TOOL) build -t ${EXPORTER_IMG} . --target=tty-exporter + $(CONTAINER_TOOL) build -t ${TRACKER_IMG} . --target=local-session-tracker .PHONY: docker-push docker-push: ## Push docker image with the manager. $(CONTAINER_TOOL) push ${PROTECTOR_IMG} - $(CONTAINER_TOOL) push ${EXPORTER_IMG} + $(CONTAINER_TOOL) push ${TRACKER_IMG} .PHONY: load-image load-image: docker-build setup kind load docker-image ${PROTECTOR_IMG} - kind load docker-image ${EXPORTER_IMG} + kind load docker-image ${TRACKER_IMG} .PHONY: build-installer diff --git a/README.md b/README.md index b39b12d..afd2040 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ curl -fsL https://github.com/cybozu-go/login-protector/releases/latest/download/ login-protector targets only StatefulSets. The StatefulSet should be configured as follows: 1. Add the label `login-protector.cybozu.io/protect: "true"` to the StatefulSet. -2. Add the sidecar container `ghcr.io/cybozu-go/tty-exporter` and specify `shareProcessNamespace: true`. +2. Add the sidecar container `ghcr.io/cybozu-go/local-session-tracker` and specify `shareProcessNamespace: true`. 3. Set the `updateStrategy` to `type: OnDelete`. @@ -71,8 +71,8 @@ spec: image: ghcr.io/cybozu/ubuntu:22.04 imagePullPolicy: IfNotPresent command: [ "sleep", "infinity" ] - - name: tty-exporter - image: ghcr.io/cybozu-go/tty-exporter:latest + - name: local-session-tracker + image: ghcr.io/cybozu-go/local-session-tracker:latest imagePullPolicy: IfNotPresent ports: - name: sidecar @@ -86,8 +86,8 @@ spec: Annotations can be used to modify the behavior of login-protector for the target StatefulSet: -- `login-protector.cybozu.io/exporter-name`: Specify the name of the tty-exporter sidecar container. Default is "tty-exporter". -- `login-protector.cybozu.io/exporter-port`: Specify the port of the tty-exporter sidecar container. Default is "8080". +- `login-protector.cybozu.io/tracker-name`: Specify the name of the local-session-tracker sidecar container. Default is "local-session-tracker". +- `login-protector.cybozu.io/tracker-port`: Specify the port of the local-session-tracker sidecar container. Default is "8080". - `login-protector.cybozu.io/no-pdb`: Set to "true" to prevent the creation of a PodDisruptionBudget. @@ -99,8 +99,8 @@ metadata: labels: login-protector.cybozu.io/protect: "true" annotations: - login-protector.cybozu.io/exporter-name: sidecar - login-protector.cybozu.io/exporter-port: "9090" + login-protector.cybozu.io/tracker-name: sidecar + login-protector.cybozu.io/tracker-port: "9090" login-protector.cybozu.io/no-pdb: "true" spec: replicas: 1 @@ -119,7 +119,7 @@ spec: imagePullPolicy: IfNotPresent command: [ "sleep", "infinity" ] - name: sidecar - image: ghcr.io/cybozu-go/tty-exporter:latest + image: ghcr.io/cybozu-go/local-session-tracker:latest imagePullPolicy: IfNotPresent ports: - name: sidecar diff --git a/Tiltfile b/Tiltfile index 30c00e6..ce2a1b2 100644 --- a/Tiltfile +++ b/Tiltfile @@ -6,10 +6,10 @@ COPY ./bin/login-protector / CMD ["/login-protector"] ''' -EXPORTER_DOCKERFILE = '''FROM golang:alpine +TRACKER_DOCKERFILE = '''FROM golang:alpine WORKDIR / -COPY ./bin/tty-exporter / -CMD ["/tty-exporter"] +COPY ./bin/local-session-tracker / +CMD ["/local-session-tracker"] ''' # Generate manifests @@ -39,11 +39,11 @@ docker_build_with_restart( k8s_yaml("./test/testdata/statefulset.yaml") docker_build_with_restart( - 'tty-exporter:dev', '.', - dockerfile_contents=EXPORTER_DOCKERFILE, - entrypoint=['/tty-exporter', '--zap-devel=true'], - only=['./bin/tty-exporter'], + 'local-session-tracker:dev', '.', + dockerfile_contents=TRACKER_DOCKERFILE, + entrypoint=['/local-session-tracker', '--zap-devel=true'], + only=['./bin/local-session-tracker'], live_update=[ - sync('./bin/tty-exporter', '/tty-exporter'), + sync('./bin/local-session-tracker', '/local-session-tracker'), ] ) diff --git a/cmd/tty-exporter/main.go b/cmd/local-session-tracker/main.go similarity index 85% rename from cmd/tty-exporter/main.go rename to cmd/local-session-tracker/main.go index ff2f642..2622a51 100644 --- a/cmd/tty-exporter/main.go +++ b/cmd/local-session-tracker/main.go @@ -10,7 +10,7 @@ import ( "syscall" "github.com/cybozu-go/login-protector/internal/common" - tty_exporter "github.com/cybozu-go/login-protector/internal/tty-exporter" + local_session_tracker "github.com/cybozu-go/login-protector/internal/local-session-tracker" "github.com/prometheus/client_golang/prometheus/promhttp" "go.uber.org/zap" ) @@ -28,9 +28,9 @@ func newZapLogger() *zap.Logger { func main() { logger := newZapLogger() defer logger.Sync() //nolint:errcheck - logger.Info("starting ttypdb-sidecar...") + logger.Info("starting local-session-tracker...") - tty_exporter.InitMetrics(logger) + local_session_tracker.InitMetrics(logger) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -52,7 +52,7 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("/readyz", handleReadyz) mux.Handle("/metrics", promhttp.Handler()) - mux.Handle("/status", tty_exporter.NewStatusHandler(logger)) + mux.Handle("/status", local_session_tracker.NewStatusHandler(logger)) server := http.Server{ Addr: fmt.Sprintf(":%d", httpServerPort), Handler: common.NewProxyHTTPHandler(mux, logger), diff --git a/internal/common/constants.go b/internal/common/constants.go index ea6dc4e..81941b8 100644 --- a/internal/common/constants.go +++ b/internal/common/constants.go @@ -2,8 +2,8 @@ package common const LabelKeyLoginProtectorProtect = "login-protector.cybozu.io/protect" const AnnotationKeyNoPDB = "login-protector.cybozu.io/no-pdb" -const AnnotationKeyExporterName = "login-protector.cybozu.io/exporter-name" -const AnnotationKeyExporterPort = "login-protector.cybozu.io/exporter-port" +const AnnotationKeyTrackerName = "login-protector.cybozu.io/tracker-name" +const AnnotationKeyTrackerPort = "login-protector.cybozu.io/tracker-port" const ValueTrue = "true" const KindStatefulSet = "StatefulSet" diff --git a/internal/controller/pdb_controller.go b/internal/controller/pdb_controller.go index 7b52b61..33baf6d 100644 --- a/internal/controller/pdb_controller.go +++ b/internal/controller/pdb_controller.go @@ -53,13 +53,13 @@ func (r *PDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R return ctrl.Result{}, err } - exporterName := "tty-exporter" - if name, ok := sts.Annotations[common.AnnotationKeyExporterName]; ok { - exporterName = name + trackerName := "local-session-tracker" + if name, ok := sts.Annotations[common.AnnotationKeyTrackerName]; ok { + trackerName = name } - exporterPort := "8080" - if port, ok := sts.Annotations[common.AnnotationKeyExporterPort]; ok { - exporterPort = port + trackerPort := "8080" + if port, ok := sts.Annotations[common.AnnotationKeyTrackerPort]; ok { + trackerPort = port } noPDB := false if noPDBStr, ok := sts.Annotations[common.AnnotationKeyNoPDB]; ok { @@ -68,7 +68,7 @@ func (r *PDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R errorList := make([]error, 0) for _, pod := range podList.Items { - err = r.reconcilePDB(ctx, &pod, exporterName, exporterPort, noPDB) + err = r.reconcilePDB(ctx, &pod, trackerName, trackerPort, noPDB) if err != nil { errorList = append(errorList, err) } @@ -81,7 +81,7 @@ func (r *PDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R return ctrl.Result{}, nil } -func (r *PDBReconciler) reconcilePDB(ctx context.Context, pod *corev1.Pod, exporterName string, exporterPort string, noPDB bool) error { +func (r *PDBReconciler) reconcilePDB(ctx context.Context, pod *corev1.Pod, trackerName string, trackerPort string, noPDB bool) error { logger := log.FromContext(ctx) if pod.DeletionTimestamp != nil { @@ -114,17 +114,17 @@ func (r *PDBReconciler) reconcilePDB(ctx context.Context, pod *corev1.Pod, expor var container *corev1.Container for _, c := range pod.Spec.Containers { c := c - if c.Name == exporterName { + if c.Name == trackerName { container = &c break } } if container == nil { - err := fmt.Errorf("failed to find sidecar container (Name: %s)", exporterName) + err := fmt.Errorf("failed to find sidecar container (Name: %s)", trackerName) return err } - resp, err := http.Get(fmt.Sprintf("http://%s:%s/status", podIP, exporterPort)) + resp, err := http.Get(fmt.Sprintf("http://%s:%s/status", podIP, trackerPort)) if err != nil { return err } diff --git a/internal/tty-exporter/metrics.go b/internal/local-session-tracker/metrics.go similarity index 86% rename from internal/tty-exporter/metrics.go rename to internal/local-session-tracker/metrics.go index f0e3b64..459e07b 100644 --- a/internal/tty-exporter/metrics.go +++ b/internal/local-session-tracker/metrics.go @@ -1,4 +1,4 @@ -package tty_exporter +package local_session_tracker import ( "math" @@ -7,7 +7,7 @@ import ( "go.uber.org/zap" ) -const metricsNamespace = "tty_exporter" +const metricsNamespace = "local_session_tracker" func InitMetrics(logger *zap.Logger) { prometheus.MustRegister(prometheus.NewGaugeFunc( diff --git a/internal/tty-exporter/server.go b/internal/local-session-tracker/server.go similarity index 96% rename from internal/tty-exporter/server.go rename to internal/local-session-tracker/server.go index e3aebf3..d92b3ba 100644 --- a/internal/tty-exporter/server.go +++ b/internal/local-session-tracker/server.go @@ -1,4 +1,4 @@ -package tty_exporter +package local_session_tracker import ( "encoding/json" diff --git a/internal/tty-exporter/exporter.go b/internal/local-session-tracker/tracker.go similarity index 98% rename from internal/tty-exporter/exporter.go rename to internal/local-session-tracker/tracker.go index b95a903..3d0738e 100644 --- a/internal/tty-exporter/exporter.go +++ b/internal/local-session-tracker/tracker.go @@ -1,4 +1,4 @@ -package tty_exporter +package local_session_tracker import ( "errors" diff --git a/test/testdata/statefulset.yaml b/test/testdata/statefulset.yaml index 94e7408..7afa021 100644 --- a/test/testdata/statefulset.yaml +++ b/test/testdata/statefulset.yaml @@ -21,8 +21,8 @@ spec: image: ghcr.io/cybozu/ubuntu:22.04 imagePullPolicy: IfNotPresent command: [ "sleep", "infinity" ] - - name: tty-exporter - image: ghcr.io/cybozu-go/tty-exporter:0.0.1 + - name: local-session-tracker + image: local-session-tracker:dev imagePullPolicy: IfNotPresent ports: - name: sidecar @@ -51,8 +51,8 @@ spec: image: ghcr.io/cybozu/ubuntu:22.04 imagePullPolicy: IfNotPresent command: [ "sleep", "infinity" ] - - name: tty-exporter - image: ghcr.io/cybozu-go/tty-exporter:0.0.1 + - name: local-session-tracker + image: local-session-tracker:dev imagePullPolicy: IfNotPresent ports: - name: sidecar