Skip to content
This repository was archived by the owner on Jun 12, 2023. It is now read-only.

Commit 709fcdd

Browse files
authored
Merge pull request #195 from omeryahud/backport-operator-upgrade
[Backport] Merge pull request #186 - Operator upgrade path
2 parents 04868cf + a34e077 commit 709fcdd

File tree

12 files changed

+367
-81
lines changed

12 files changed

+367
-81
lines changed

_defaults.yml

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ virt_launcher_tag: "{{ lookup('env','VIRT_LAUNCHER_TAG')| default('v0.21.0', tru
2727
validator_tag: "{{ lookup('env','VALIDATOR_TAG')| default('v0.6.6', true) }}"
2828
image_name_prefix: "{{ lookup('env','IMAGE_NAME_PREFIX')| default('', true) }}"
2929
templates_version: v0.11.2
30+
operator_version: "{{ lookup('env', 'OPERATOR_VERSION') }}"

build/csv-generator.sh

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ replace_env_var() {
2121
}
2222

2323
help_text() {
24-
echo "USAGE: csv-generator --csv-version=<version> --namespace=<namespace> --operator-image=<operator image> [optional args]"
24+
echo "USAGE: csv-generator --csv-version=<version> --namespace=<namespace> --operator-image=<operator image> --operator-version=<operator version> [optional args]"
2525
echo ""
2626
echo "ARGS:"
2727
echo " --csv-version: (REQUIRED) The version of the CSV file"
2828
echo " --namespace: (REQUIRED) The namespace set on the CSV file"
2929
echo " --operator-image: (REQUIRED) The operator container image to use in the CSV file"
30+
echo " --operator-version: (REQUIRED) The version of the operator to use in the CSV file"
3031
echo " --watch-namespace: (OPTIONAL)"
3132
echo " --kvm-info-tag: (OPTIONAL)"
3233
echo " --validator-tag: (OPTIONAL)"
@@ -41,6 +42,7 @@ help_text() {
4142
CSV_VERSION=""
4243
NAMESPACE=""
4344
OPERATOR_IMAGE=""
45+
OPERATOR_VERSION=""
4446

4547
# OPTIONAL ARGS
4648
WATCH_NAMESPACE=""
@@ -67,6 +69,9 @@ while (( "$#" )); do
6769
--operator-image)
6870
OPERATOR_IMAGE=$VAL
6971
;;
72+
--operator-version)
73+
OPERATOR_VERSION=$VAL
74+
;;
7075
--watch-namespace)
7176
WATCH_NAMESPACE=$VAL
7277
;;
@@ -101,7 +106,7 @@ while (( "$#" )); do
101106
esac
102107
done
103108

104-
if [ -z "$CSV_VERSION" ] || [ -z "$NAMESPACE" ] || [ -z "$OPERATOR_IMAGE" ]; then
109+
if [ -z "$CSV_VERSION" ] || [ -z "$NAMESPACE" ] || [ -z "$OPERATOR_IMAGE" ] || [ -z "$OPERATOR_VERSION" ]; then
105110
echo "Error: Missing required arguments"
106111
help_text
107112
exit 1
@@ -114,6 +119,7 @@ cp ${MANIFESTS_GENERATED_CSV} ${TMP_FILE}
114119
sed -i "s/PLACEHOLDER_CSV_VERSION/${CSV_VERSION}/g" ${TMP_FILE}
115120
sed -i "s/namespace: placeholder/namespace: ${NAMESPACE}/g" ${TMP_FILE}
116121
sed -i "s|REPLACE_IMAGE|${OPERATOR_IMAGE}|g" ${TMP_FILE}
122+
sed -i "s|REPLACE_VERSION|${OPERATOR_VERSION}|g" ${TMP_FILE}
117123

118124
replace_env_var "WATCH_NAMESPACE" $WATCH_NAMESPACE
119125
replace_env_var "KVM_INFO_TAG" $KVM_INFO_TAG

deploy/operator.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ spec:
5050
value: ""
5151
- name: OPERATOR_NAME
5252
value: "kubevirt-ssp-operator"
53+
- name: OPERATOR_VERSION
54+
value: REPLACE_VERSION

deploy/role.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rules:
1616
- patch
1717
- update
1818
- watch
19+
- delete
1920
- apiGroups:
2021
- monitoring.coreos.com
2122
resources:
@@ -26,6 +27,7 @@ rules:
2627
- list
2728
- patch
2829
- watch
30+
- delete
2931
- apiGroups:
3032
- monitoring.coreos.com
3133
resources:
@@ -67,6 +69,7 @@ rules:
6769
- list
6870
- patch
6971
- watch
72+
- delete
7073
- apiGroups:
7174
- ""
7275
resources:
@@ -81,6 +84,7 @@ rules:
8184
- patch
8285
- list
8386
- watch
87+
- delete
8488
- apiGroups:
8589
- ""
8690
resources:

hack/make-manifests.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ CHANNEL="beta"
1212
CLUSTER_VERSIONED_DIR="cluster/${VERSION}"
1313
MANIFESTS_DIR="manifests/kubevirt-ssp-operator"
1414
MANIFESTS_VERSIONED_DIR="${MANIFESTS_DIR}/${TAG}"
15-
IMAGE_PATH="quay.io/fromani/kubevirt-ssp-operator-container:latest"
15+
IMAGE_PATH="quay.io/fromani/kubevirt-ssp-operator-container:${TAG}"
1616

1717
HAVE_COURIER=0
1818
if which operator-courier &> /dev/null; then
@@ -39,11 +39,11 @@ done
3939
(
4040
for MF in deploy/service_account.yaml deploy/role.yaml deploy/role_binding.yaml deploy/operator.yaml; do
4141
echo "---"
42-
sed "s|REPLACE_IMAGE|${IMAGE_PATH}|" < ${MF}
42+
sed "s|REPLACE_IMAGE|${IMAGE_PATH}|g ; s|REPLACE_VERSION|${TAG}|g" < ${MF}
4343
done
4444
) > ${CLUSTER_VERSIONED_DIR}/kubevirt-ssp-operator.yaml
4545

46-
${BASEPATH}/../build/csv-generator.sh --csv-version=${VERSION} --namespace=placeholder --operator-image=REPLACE_IMAGE > ${MANIFESTS_VERSIONED_DIR}/kubevirt-ssp-operator.${TAG}.clusterserviceversion.yaml
46+
${BASEPATH}/../build/csv-generator.sh --csv-version=${VERSION} --namespace=placeholder --operator-image=REPLACE_IMAGE --operator-version=REPLACE_VERSION > ${MANIFESTS_VERSIONED_DIR}/kubevirt-ssp-operator.${TAG}.clusterserviceversion.yaml
4747

4848
# caution: operator-courier (as in 5a4852c) wants *one* entity per yaml file (e.g. it does NOT use safe_load_all)
4949
for CRD in $( ls deploy/crds/kubevirt_*crd.yaml ); do

roles/ClaimOwnership/tasks/main.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
# tasks file for ClaimOwnership
3+
- name: Claim Ownership
4+
k8s:
5+
state: present
6+
resource_definition:
7+
apiVersion: "{{ object.apiVersion }}"
8+
kind: "{{ object.kind }}"
9+
metadata:
10+
name: "{{ object.metadata.name }}"
11+
namespace: "{{ object.metadata.namespace }}"
12+
ownerReferences:
13+
- apiVersion: "{{ owner.apiVersion }}"
14+
kind: "{{ owner.kind }}"
15+
name: "{{ owner.metadata.name }}"
16+
uid: "{{ owner.metadata.uid }}"

roles/KubevirtCircuitBreaker/tasks/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# tasks file for KubevirtCircuitBreaker
33
- name: Extract the CR info
44
set_fact:
5-
cr_info: "{{ lookup('k8s', api_version='v1', kind=cr_kind, namespace=meta.namespace, resource_name=meta.name) | from_yaml }}"
5+
cr_info: "{{ lookup('k8s', api_version='ssp.kubevirt.io/v1', kind=cr_kind, namespace=meta.namespace, resource_name=meta.name) | from_yaml }}"
66
- name: Extract the disable info
77
set_fact:
88
is_paused: "{{ cr_info['metadata'].get('annotations', {})['kubevirt.io/operator.paused'] | default('false', true) | from_json }}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from ansible.errors import AnsibleError
2+
3+
class FilterModule(object):
4+
def filters(self):
5+
return {
6+
'k8s_owned_by': k8s_owned_by
7+
}
8+
9+
def k8s_owned_by(objects, owner):
10+
owned = []
11+
12+
for obj in objects:
13+
if object_owned_by(obj, owner):
14+
owned.append(obj)
15+
16+
return owned
17+
18+
def object_owned_by(object, owner):
19+
if owner is None:
20+
raise AnsibleError('owner is empty')
21+
if 'metadata' not in owner:
22+
raise AnsibleError('owner is missing "metadata" field')
23+
if 'uid' not in owner['metadata']:
24+
raise AnsibleError('owner is missing "metadata.uid" field')
25+
26+
if object is None:
27+
raise AnsibleError('object is empty')
28+
if 'metadata' not in object:
29+
raise AnsibleError('object is missing "metadata" field')
30+
if 'ownerReferences' not in object['metadata']:
31+
return False
32+
33+
ownerUID = owner['metadata']['uid']
34+
35+
if object['metadata']['ownerReferences'] is not None:
36+
for ref in object['metadata']['ownerReferences']:
37+
if ref['uid'] == ownerUID:
38+
return True
39+
40+
return False
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,113 @@
11
---
2+
- name: Set operatorVersion and targetVersion
3+
operator_sdk.util.k8s_status:
4+
api_version: "{{ cr_info.apiVersion }}"
5+
kind: "{{ cr_info.kind }}"
6+
name: "{{ cr_info.metadata.name }}"
7+
namespace: "{{ cr_info.metadata.namespace }}"
8+
status:
9+
operatorVersion: "{{ operator_version }}"
10+
targetVersion: "{{ operator_version }}"
11+
212
# tasks file for KubevirtCommonTemplatesBundle
313
- name: Install VM templates
414
k8s:
515
state: present
6-
namespace: "{{ meta.namespace }}"
16+
namespace: "{{ cr_info.metadata.namespace }}"
717
definition: "{{ item | from_yaml }}"
18+
apply: yes
819
with_items: "{{ lookup('file', 'common-templates-'+ version +'.yaml').split('\n---\n') | select('search', '(^|\n)[^#]') | list }}"
920
register: ct_status
1021

22+
# Get all templates
23+
- name: Fetching all templates
24+
set_fact:
25+
templates: "{{ lookup('k8s', api_version=ct_status.results[0].result.apiVersion, kind='template') }}"
26+
27+
- name: Fetch old cr
28+
block:
29+
- set_fact:
30+
old_cr: "{{ lookup('k8s', api_version='kubevirt.io/v1', kind='KubevirtCommonTemplatesBundle') }}"
31+
- set_fact:
32+
old_cr_exists: true
33+
rescue:
34+
- set_fact:
35+
old_cr_exists: false
36+
37+
- name: Filter for templates owned by the old cr
38+
set_fact:
39+
old_cr_templates: "{{ templates | k8s_owned_by(old_cr) }}"
40+
when: "{{ old_cr_exists==true }}"
41+
42+
# Inject ownerReferences
43+
- name: Inject owner references for KubevirtCommonTemplatesBundle
44+
include_role:
45+
name: ClaimOwnership
46+
vars:
47+
object: "{{ item }}"
48+
owner: "{{ cr_info }}"
49+
when: "{{ old_cr_exists==true }}"
50+
with_items: "{{ old_cr_templates }}" # Templates
51+
1152
- name: "Count all new templates in file"
1253
set_fact:
1354
new_templates: "{{ lookup('file', 'common-templates-'+ version +'.yaml').split('\n---\n') | select('search', '(^|\n)[^#]') | list | length }}"
1455

15-
1656
- name: "Set label"
1757
set_fact:
1858
label: "template.kubevirt.io/version={{ version }}"
19-
59+
2060
- name: "Get all templates"
2161
set_fact:
22-
deployed_templates_after: "{{ lookup('k8s', api_version=ct_status.results[0].result.apiVersion, kind='template', label_selector=label)|length }}"
62+
deployed_templates_after: "{{ lookup('k8s', api_version=ct_status.results[0].result.apiVersion, kind='template', label_selector=label) | length }}"
63+
64+
- name: "Set Available status"
65+
set_fact:
66+
available: "{{ true if new_templates <= deployed_templates_after else false }}"
2367

2468
- name: Set progressing condition
2569
operator_sdk.util.k8s_status:
26-
api_version: ssp.kubevirt.io/v1
27-
kind: KubevirtCommonTemplatesBundle
28-
name: "{{ meta.name }}"
29-
namespace: "{{ meta.namespace }}"
70+
api_version: "{{ cr_info.apiVersion }}"
71+
kind: "{{ cr_info.kind }}"
72+
name: "{{ cr_info.metadata.name }}"
73+
namespace: "{{ cr_info.metadata.namespace }}"
3074
conditions:
3175
- type: Progressing
32-
status: "{{ 'True' if new_templates > deployed_templates_after else 'False' }}"
76+
status: "False"
3377
reason: "progressing"
34-
message: "Templates progressing."
78+
message: "Templates progressing (deployed templates: {{ deployed_templates_after }}, desired deployed templated: {{ new_templates }})."
3579

3680
- name: Set available condition
3781
operator_sdk.util.k8s_status:
38-
api_version: ssp.kubevirt.io/v1
39-
kind: KubevirtCommonTemplatesBundle
40-
name: "{{ meta.name }}"
41-
namespace: "{{ meta.namespace }}"
82+
api_version: "{{ cr_info.apiVersion }}"
83+
kind: "{{ cr_info.kind }}"
84+
name: "{{ cr_info.metadata.name }}"
85+
namespace: "{{ cr_info.metadata.namespace }}"
4286
conditions:
4387
- type: Available
44-
status: "{{ 'True' if new_templates <= deployed_templates_after else 'False' }}"
88+
status: "True"
4589
reason: "available"
46-
message: "Common templates available."
90+
message: "Common templates available (deployed templates: {{ deployed_templates_after }}, desired deployed templated: {{ new_templates }})."
91+
92+
- name: Set degraded condition
93+
operator_sdk.util.k8s_status:
94+
api_version: "{{ cr_info.apiVersion }}"
95+
kind: "{{ cr_info.kind }}"
96+
name: "{{ cr_info.metadata.name }}"
97+
namespace: "{{ cr_info.metadata.namespace }}"
98+
conditions:
99+
- type: Degraded
100+
status: "False"
101+
reason: "degraded"
102+
message: "Templates degraded (deployed templates: {{ deployed_templates_after }}, desired deployed templated: {{ new_templates }})."
103+
104+
# Update observerVersion when the CR is Available to indicate a successfull upgrade
105+
- name: Set observedVersion
106+
operator_sdk.util.k8s_status:
107+
api_version: "{{ cr_info.apiVersion }}"
108+
kind: "{{ cr_info.kind }}"
109+
name: "{{ cr_info.metadata.name }}"
110+
namespace: "{{ cr_info.metadata.namespace }}"
111+
status:
112+
observedVersion: "{{ operator_version }}"
113+
when: "{{ available==true }}"
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,75 @@
11
---
2+
- name: Set operatorVersion and targetVersion
3+
operator_sdk.util.k8s_status:
4+
api_version: "{{ cr_info.apiVersion }}"
5+
kind: "{{ cr_info.kind }}"
6+
name: "{{ cr_info.metadata.name }}"
7+
namespace: "{{ cr_info.metadata.namespace }}"
8+
status:
9+
operatorVersion: "{{ operator_version }}"
10+
targetVersion: "{{ operator_version }}"
11+
212
# tasks file for KubevirtMetricsAggregation
313
- name: Install VMI count aggregation rule
414
k8s:
515
state: present
616
namespace: "{{ meta.namespace }}"
717
definition: "{{ item | from_yaml }}"
18+
apply: yes
819
with_items: "{{ lookup('template', 'aggregation-rule-vmi-count.yaml.j2').split('\n---\n') | select('search', '(^|\n)[^#]') |list }}"
20+
register: promrules
21+
22+
- name: Inject owner references for KubevirtMetricsAggregation
23+
include_role:
24+
name: ClaimOwnership
25+
vars:
26+
object: "{{ item.result }}"
27+
owner: "{{ cr_info }}"
28+
with_items: "{{ promrules.results }}"
29+
30+
- name: Set available condition
31+
operator_sdk.util.k8s_status:
32+
api_version: "{{ cr_info.apiVersion }}"
33+
kind: "{{ cr_info.kind }}"
34+
name: "{{ cr_info.metadata.name }}"
35+
namespace: "{{ cr_info.metadata.namespace }}"
36+
conditions:
37+
- type: Available
38+
status: "True"
39+
reason: "available"
40+
message: "KubevirtMetricsAggregation is available."
41+
42+
# There is no logic for the Progressing/Degraded conditions for KubevirtMetricsAggregation
43+
# so using these constant conditions
44+
- name: Set progressing condition
45+
operator_sdk.util.k8s_status:
46+
api_version: "{{ cr_info.apiVersion }}"
47+
kind: "{{ cr_info.kind }}"
48+
name: "{{ cr_info.metadata.name }}"
49+
namespace: "{{ cr_info.metadata.namespace }}"
50+
conditions:
51+
- type: Progressing
52+
status: "False"
53+
reason: "progressing"
54+
message: "KubevirtMetricsAggregation progressing"
55+
56+
- name: Set degraded condition
57+
operator_sdk.util.k8s_status:
58+
api_version: "{{ cr_info.apiVersion }}"
59+
kind: "{{ cr_info.kind }}"
60+
name: "{{ cr_info.metadata.name }}"
61+
namespace: "{{ cr_info.metadata.namespace }}"
62+
conditions:
63+
- type: Degraded
64+
status: "False"
65+
reason: "degraded"
66+
message: "KubevirtMetricsAggregation degraded"
967

68+
- name: Set observedVersion
69+
operator_sdk.util.k8s_status:
70+
api_version: "{{ cr_info.apiVersion }}"
71+
kind: "{{ cr_info.kind }}"
72+
name: "{{ cr_info.metadata.name }}"
73+
namespace: "{{ cr_info.metadata.namespace }}"
74+
status:
75+
observedVersion: "{{ operator_version }}"

0 commit comments

Comments
 (0)