1+ #! /usr/bin/env bash
2+ # Manage kubectl plugin binaries as OCI artifacts.
3+ # Supports both pushing and pulling operations.
4+ # Usage:
5+ # Push: ./kubectl-oci.sh push --tag <tag> --namespace <namespace> --username <user> --password <token>
6+ # Pull: ./kubectl-oci.sh pull --tag <tag> --namespace <namespace> --username <user> --password <token>
7+
8+ set -euo pipefail
9+
10+ SCRIPT_DIR=" $( dirname " $( realpath " ${BASH_SOURCE[0]:- " $0 " } " ) " ) "
11+ ROOT_DIR=" $SCRIPT_DIR /../"
12+
13+ source " $ROOT_DIR /utils/log.sh"
14+
15+ ACTION=" "
16+ TAG=" "
17+ NAMESPACE=" "
18+ USERNAME=" "
19+ PASSWORD=" "
20+ REGISTRY=" ghcr.io"
21+ PLUGIN=" ${PLUGIN:- " mayastor" } "
22+
23+ usage () {
24+ cat << EOF
25+ Usage: $0 <action> [options]
26+
27+ Actions:
28+ push Push kubectl binaries to $REGISTRY as OCI artifacts
29+ pull Pull kubectl binaries from $REGISTRY OCI artifacts
30+
31+ Options:
32+ --tag <tag> Release tag (required)
33+ --registry <registry> The registry to push/pull from [default=$REGISTRY ]
34+ --namespace <namespace> Namespace path (required)
35+ --username <username> Registry username (required)
36+ --password <password> Registry token/password (required)
37+
38+ Examples:
39+ $0 push --tag v1.0.0 --namespace $PLUGIN /dev --username user --password token
40+ $0 pull --tag v1.0.0 --namespace $PLUGIN /dev --username user --password token
41+ EOF
42+ }
43+
44+ parse_args () {
45+ if [[ $# -lt 1 ]]; then
46+ usage
47+ log_fatal " Error: Action required (push or pull)"
48+ fi
49+
50+ ACTION=" $1 "
51+ shift
52+
53+ case " $ACTION " in
54+ push|pull)
55+ ;;
56+ * )
57+ usage
58+ log_fatal " Error: Invalid action '$ACTION '. Must be 'push' or 'pull'"
59+ ;;
60+ esac
61+
62+ while [[ $# -gt 0 ]]; do
63+ case $1 in
64+ --tag)
65+ TAG=" $2 "
66+ shift 2
67+ ;;
68+ --namespace)
69+ NAMESPACE=" $2 "
70+ shift 2
71+ ;;
72+ --registry)
73+ REGISTRY=" $2 "
74+ shift 2
75+ ;;
76+ --username)
77+ USERNAME=" $2 "
78+ shift 2
79+ ;;
80+ --password)
81+ PASSWORD=" $2 "
82+ shift 2
83+ ;;
84+ -h|--help)
85+ usage
86+ exit 0
87+ ;;
88+ * )
89+ usage
90+ log_fatal " Unknown option: $1 "
91+ ;;
92+ esac
93+ done
94+
95+ if [[ -z " $TAG " ]] || [[ -z " $NAMESPACE " ]] || [[ -z " $USERNAME " ]] || [[ -z " $PASSWORD " ]]; then
96+ usage
97+ log_fatal " Error: All options (--tag, --namespace, --username, --password) are required"
98+ fi
99+
100+ REPOSITORY=${REGISTRY} /${NAMESPACE} /kubectl-${PLUGIN}
101+ }
102+
103+ # Login to registry
104+ login_registry () {
105+ echo " Logging in to ${REGISTRY} ..."
106+ echo " ${PASSWORD} " | oras login " ${REGISTRY} " --username " ${USERNAME} " --password-stdin
107+ }
108+
109+ # Push kubectl binaries to registry
110+ push_artifacts () {
111+ echo " Pushing kubectl binaries to ${REPOSITORY} with tag ${TAG} "
112+
113+ # Check if artifacts directory exists
114+ if [[ ! -d " artifacts" ]]; then
115+ log_fatal " Error: artifacts directory not found"
116+ fi
117+
118+ # Create a combined tarball of all kubectl binaries
119+ echo " Creating combined tarball of all kubectl binaries..."
120+ local combined_tar=" kubectl-$PLUGIN -all-platforms-${TAG} .tar.gz"
121+
122+ tar -czf " ${combined_tar} " -C artifacts .
123+
124+ echo " Pushing combined tarball to ${REPOSITORY} :${TAG} "
125+
126+ oras push " ${REPOSITORY} :${TAG} " \
127+ --artifact-type application/vnd.$PLUGIN .kubectl.bundle.v1+tar+gzip \
128+ " ${combined_tar} "
129+
130+ rm -f " ${combined_tar} "
131+
132+ echo " ✓ All kubectl binaries pushed successfully as a single bundle!"
133+ echo " Bundle available at: ${REPOSITORY} :${TAG} "
134+ }
135+
136+ # Pull kubectl binaries from registry
137+ pull_artifacts () {
138+ echo " Pulling kubectl binaries bundle from ${REPOSITORY} for release ${TAG} "
139+
140+ echo " Pulling kubectl bundle..."
141+ oras pull " ${REPOSITORY} :${TAG} "
142+
143+ local bundle_tar=" kubectl-$PLUGIN -all-platforms-${TAG} .tar.gz"
144+
145+ if [[ ! -f " $bundle_tar " ]]; then
146+ log_fatal " Error: Could not find kubectl bundle tarball"
147+ fi
148+
149+ echo " Extracting bundle to artifacts directory"
150+
151+ mkdir -p artifacts/
152+ tar -xzf " $bundle_tar " -C artifacts/
153+ rm -f " $bundle_tar "
154+
155+ echo " Contents of artifacts directory after extraction:"
156+ ls -la artifacts/
157+
158+ echo " ✓ All kubectl binaries pulled successfully!"
159+ }
160+
161+ main () {
162+ parse_args " $@ "
163+ login_registry
164+
165+ case " $ACTION " in
166+ push)
167+ push_artifacts
168+ ;;
169+ pull)
170+ pull_artifacts
171+ ;;
172+ esac
173+ }
174+
175+ main " $@ "
0 commit comments