init repo

This commit is contained in:
bryan 2024-05-25 12:57:03 +08:00
parent 359f914290
commit 3d0585a522
9 changed files with 251 additions and 16 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
/release/*.tar.gz filter=lfs diff=lfs merge=lfs -text

17
.gitignore vendored
View File

@ -4,20 +4,5 @@
*.so
# Folders
_obj
_test
_output
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test

22
Makefile Normal file
View File

@ -0,0 +1,22 @@
###################################################
# Makefile for yeahka devtestops platform #########
###################################################
SHELL=/bin/bash
###################################################
APPNAME ?= kubectl-plslogs
APPVERSION ?= v0.1.0
PKGNAME ?= $(APPNAME)-$(APPVERSION).tar.gz
BUILD_DIR = $(shell pwd)
TEMP_OUTPUT_DIR = $(shell pwd)/_output/$(APPNAME)
build-plslogs:
make build -C src/plslogs/
release-plslogs:
make release -C src/plslogs/
clean:
make clean -C src/plslogs/

28
plugins/plslogs.yaml Normal file
View File

@ -0,0 +1,28 @@
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
name: plslogs
spec:
homepage: https://git.qoobing.com/opensource/kubernetes/kubectl-plugins.git
shortDescription: "pls(pod list select) and then logs(kubectl logs)"
version: "v0.1.0"
description: |
Provide a simple tools for logs pod without 'copy' pod name.
It show a pod list first and after you choose one,
and it will execute 'kubectl logs --tail 100 -f $selectpod'
platforms:
- selector:
matchExpressions:
- key: "os"
operator: "In"
values:
- darwin
- linux
uri: https://github.com/morningspace/kubeassert/archive/v0.2.0.tar.gz
sha256: a35b62a111212a74c954f2991fdfa7b4cad8e92b9318773f87c9ff8c12a5ea52
bin: kubectl-assert.sh
files:
- from: "/kubeassert-*/kubectl-assert.sh"
to: "."
- from: "/kubeassert-*/LICENSE"
to: "."

1
release/README Normal file
View File

@ -0,0 +1 @@
All kubectl-plugins releases tar.

21
src/plslogs/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 MorningSpace
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

47
src/plslogs/Makefile Normal file
View File

@ -0,0 +1,47 @@
###################################################
# Makefile for yeahka devtestops platform #########
###################################################
SHELL=/bin/bash
###################################################
APPNAME ?= kubectl-plslogs
APPVERSION ?= v0.1.0
PKGNAME ?= $(APPNAME)-$(APPVERSION).tar.gz
BUILD_DIR = $(shell pwd)
TEMP_OUTPUT_DIR = $(shell pwd)/_output/$(APPNAME)-$(APPVERSION)
build:
@echo -e "======\033[44m start to build binary... \033[0m"
@echo -e "BUILD do NOTHING"
@echo -e "\033[42mbuild binary done\033[0m"
package:
@echo -e "======\033[44m start to clean old output directory... \033[0m"
@-rm -rf $(TEMP_OUTPUT_DIR) >/dev/null 2>&1
mkdir -p $(TEMP_OUTPUT_DIR)/{bin,lib,etc,logs} >/dev/null 2>&1
echo -e "clean old output directory done"
@echo -e "======\033[44m start to copy files to output directory...\033[0m"
cp -rL $(BUILD_DIR)/plslogs.sh $(TEMP_OUTPUT_DIR)/
cp -rL $(BUILD_DIR)/LICENSE $(TEMP_OUTPUT_DIR)/
@echo -e "copy files to \"$(TEMP_OUTPUT_DIR)\" done"
@echo -e "======\033[34m start to package tar...\033[0m"
cd $(TEMP_OUTPUT_DIR)/.. && tar -czf $(PKGNAME) "$(APPNAME)-$(APPVERSION)"
@echo -e "tar: \033[33m `realpath $(TEMP_OUTPUT_DIR)/..`/$(PKGNAME)\033[0m"
@echo -e "\033[5mpackage tar done\033[0m"
clean:
@echo -e "======\033[44m start to clean \033[0m"
@-rm -rf $(BUILD_DIR)/bin/ _output
@echo “clean done
tar: build package
release: tar
@echo -e "======\033[44m start to realase $(APPNAME)-$(APPVERSION)\033[0m"
cd $(TEMP_OUTPUT_DIR)/.. && cp -f $(PKGNAME) $(TEMP_OUTPUT_DIR)/../../../../release/
@echo “clean done

107
src/plslogs/plslogs.sh Executable file
View File

@ -0,0 +1,107 @@
#!/usr/bin/env bash
function _kube_parse_args() {
# We use "${@}" instead of "${*}" to preserve argument-boundary information
options=''
args=$(getopt --options ':n:f:' --longoptions ':namespace:,tail:' -- "${@}") || exit
eval "set -- ${args}"
while true; do
case "${1}" in
(-v | --verbose)
((verbose++))
shift
;;
(-a | --article)
article=${2}
shift 2
;;
(-l | --lang | --language)
# handle optional: getopt normalizes it into an empty string
if [[ -n ${2} ]] ; then
lang=${2}
fi
shift 2
;;
(--)
shift
break
;;
(*)
exit 1 # error
;;
esac
done
remaining_args=("${@}")
}
function _kube_list_pods() {
NS_ARG="" #"--all-namespaces"
[[ -n "$1" ]] && NS_ARG="-n ${1}"
GO_TPL=""
GO_TPL="$GO_TPL"'{{range .items}}'
GO_TPL="$GO_TPL"""'{{.metadata.name}}:'
GO_TPL="$GO_TPL"""'{{.metadata.namespace}}:'
GO_TPL="$GO_TPL"""'{{.status.phase}}'
GO_TPL="$GO_TPL"""'({{range $index, $element := .status.containerStatuses}}'
GO_TPL="$GO_TPL"""""'{{if $index}}&{{end}}'
GO_TPL="$GO_TPL"""""'{{if .state.waiting}}{{.state.waiting.reason}}{{end}}'
GO_TPL="$GO_TPL"""""'{{if .state.running}}Runing{{end}}'
GO_TPL="$GO_TPL"""'{{end}})'
GO_TPL="$GO_TPL"""'{{"\n"}}'
GO_TPL="$GO_TPL"'{{end}}'
IFS=';'
read -ra pods <<< "$(kubectl get pods $NS_ARG -o go-template="$GO_TPL" | sort -k 2 -k 1 -t: | tr '\n' ';')"
local count=1
lines=$(for i in ${pods[@]}; do
IFS=":" read -ra TOKS <<< "${i}"
printf " $count) ${TOKS[0]}\t${TOKS[1]}\t${TOKS[2]}\n"
((count=count+1))
done | column -t)
count=$(echo "$lines" | wc -l)
echo "$lines" >&2
local sel=0
while [[ $sel -lt 1 || $sel -gt $count ]]; do
read -p "Select a Pod: " sel >&2
done
echo "${pods[(sel-1)]}"
}
function _kube_list_pod_containers() {
POD=$1
NAMESPACE=$2
IFS=';' read -ra items <<< "$(kubectl get pod ${POD} -n ${NAMESPACE} -o go-template='{{range .spec.containers}}{{.name}}{{"\n"}}{{end}}' | tr '\n' ';')"
local count=1
lines=$(for i in ${items[@]}; do
printf " $count) ${i}\n"
((count=count+1))
done | column -t)
count=$(echo "$lines" | wc -l)
if [[ $count -gt 1 ]]; then
printf "\nPod has multiple containers:\n" >&2
echo "$lines" >&2
local sel=0
while [[ $sel -lt 1 || $sel -gt $count ]]; do
read -p "Select a Container: " sel >&2
done
fi
echo "${items[(sel-1)]}"
}
SEL=$(_kube_list_pods)
IFS=":" read -ra POD <<< "${SEL}"
if [[ ${POD[2]} != Running* ]]; then
echo "Status:${POD[2]}" >&2
echo "ERROR: Pod ${POD[0]} is not running" >&2
exit 1
fi
SEL=$(_kube_list_pod_containers ${POD[0]} ${POD[1]})
kubectl -n ${POD[1]} logs --tail 100 "${POD[0]}" -c ${SEL} $@

23
src/plslogs/plugin.yaml Normal file
View File

@ -0,0 +1,23 @@
# Copyright 2018 Google LLC
#
# 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.
name: "plslogs"
shortDesc: "Get logs for a pod from list"
longDesc: >
Display list of all pods to select from.
If pod has multiple containers, another list is shown to choose from.
Example:
kubectl plugin plslogs
kubectl plugin plslogs -n kube-system
command: ./plslogs.sh