22# Manage kubectl plugin binaries as OCI artifacts.
33# Supports both pushing and pulling operations.
44# 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>
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>]
77
88set -euo pipefail
99
1010SCRIPT_DIR=" $( dirname " $( realpath " ${BASH_SOURCE[0]:- " $0 " } " ) " ) "
1111ROOT_DIR=" $SCRIPT_DIR /../"
1212
13+ # shellcheck source=../utils/log.sh
1314source " $ROOT_DIR /utils/log.sh"
1415
1516ACTION=" "
@@ -19,6 +20,7 @@ USERNAME=""
1920PASSWORD=" "
2021REGISTRY=" ghcr.io"
2122PLUGIN=" ${PLUGIN:- " mayastor" } "
23+ NO_LOGIN=false
2224
2325usage () {
2426 cat << EOF
@@ -32,12 +34,14 @@ Options:
3234 --tag <tag> Release tag (required)
3335 --registry <registry> The registry to push/pull from [default=$REGISTRY ]
3436 --namespace <namespace> Namespace path (required)
35- --username <username> Registry username (required)
36- --password <password> Registry token/password (required)
37+ --username <username> Registry username (optional if already logged in)
38+ --password <password> Registry token/password (optional if already logged in)
39+ --no-login Skip login check (useful for CI/CD with pre-auth)
40+ -h, --help Show this help message
3741
3842Examples:
3943 $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
44+ $0 pull --tag v1.0.0 --namespace $PLUGIN /dev
4145EOF
4246}
4347
@@ -81,6 +85,10 @@ parse_args() {
8185 PASSWORD=" $2 "
8286 shift 2
8387 ;;
88+ --no-login)
89+ NO_LOGIN=true
90+ shift
91+ ;;
8492 -h|--help)
8593 usage
8694 exit 0
@@ -92,16 +100,31 @@ parse_args() {
92100 esac
93101 done
94102
95- if [[ -z " $TAG " ]] || [[ -z " $NAMESPACE " ]] || [[ -z " $USERNAME " ]] || [[ -z " $PASSWORD " ]] ; then
103+ if [[ -z " $TAG " ]] || [[ -z " $NAMESPACE " ]]; then
96104 usage
97- log_fatal " Error: All options (--tag, --namespace, --username, --password) are required"
105+ log_fatal " Error: --tag and --namespace are required"
106+ fi
107+
108+ if [[ -z " ${USERNAME:- } " || -z " ${PASSWORD:- } " ]]; then
109+ echo " ⚠️ No username/password provided — will attempt to use existing oras login session for ${REGISTRY} "
98110 fi
99111
100- REPOSITORY=${REGISTRY} /${NAMESPACE} /kubectl-${PLUGIN}
112+ REPOSITORY=" ${REGISTRY} /${NAMESPACE} /kubectl-${PLUGIN} "
101113}
102114
103- # Login to registry
115+ # Login to registry (only if needed)
104116login_registry () {
117+ echo " Checking login status for ${REGISTRY} ..."
118+
119+ if oras login " ${REGISTRY} " --get > /dev/null 2>&1 ; then
120+ echo " Already logged in to ${REGISTRY} "
121+ return 0
122+ fi
123+
124+ if [[ -z " ${USERNAME:- } " || -z " ${PASSWORD:- } " ]]; then
125+ log_fatal " Error: Not logged in to ${REGISTRY} and no credentials provided. Use --username and --password."
126+ fi
127+
105128 echo " Logging in to ${REGISTRY} ..."
106129 echo " ${PASSWORD} " | oras login " ${REGISTRY} " --username " ${USERNAME} " --password-stdin
107130}
@@ -129,7 +152,7 @@ push_artifacts() {
129152
130153 rm -f " ${combined_tar} "
131154
132- echo " ✓ All kubectl binaries pushed successfully as a single bundle!"
155+ echo " All kubectl binaries pushed successfully as a single bundle!"
133156 echo " Bundle available at: ${REPOSITORY} :${TAG} "
134157}
135158
@@ -155,12 +178,15 @@ pull_artifacts() {
155178 echo " Contents of artifacts directory after extraction:"
156179 ls -la artifacts/
157180
158- echo " ✓ All kubectl binaries pulled successfully!"
181+ echo " All kubectl binaries pulled successfully!"
159182}
160183
161184main () {
162185 parse_args " $@ "
163- login_registry
186+
187+ if [[ " $NO_LOGIN " == false ]]; then
188+ login_registry
189+ fi
164190
165191 case " $ACTION " in
166192 push)
0 commit comments