Skip to content

Commit

Permalink
Add filter for excluding precache images (#411)
Browse files Browse the repository at this point in the history
Precaching pulls more images than it is needed. This story enables the user
to provide a list of patterns to be excluded from precaching. User can specify
"excludePrecachePatterns" field in the overrides configmap. Using the command
"grep -vG -f {patterns-file}" precaching will filter out the patterns specified.
Below is an example of override configmap with excludePrecachePatterns:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-group-upgrade-overrides
data:
  excludePrecachePatterns: |
    aws
    thanos

closes:
https://issues.redhat.com/browse/OCPBUGS-3092
https://issues.redhat.com/browse/CNF-6848

Signed-off-by: Saeid Askari <saskari@redhat.com>
  • Loading branch information
sudomakeinstall2 committed Jan 31, 2023
1 parent d72dbe6 commit 0ce58ae
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 13 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/clustergroupupgrade_types.go
Expand Up @@ -210,6 +210,7 @@ type PrecachingSpec struct {
PlatformImage string `json:"platformImage,omitempty"`
OperatorsIndexes []string `json:"operatorsIndexes,omitempty"`
OperatorsPackagesAndChannels []string `json:"operatorsPackagesAndChannels,omitempty"`
ExcludePrecachePatterns []string `json:"excludePrecachePatterns,omitempty"`
}

// PrecachingStatus defines the observed pre-caching status
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bundle/manifests/ran.openshift.io_clustergroupupgrades.yaml
Expand Up @@ -393,6 +393,10 @@ spec:
description: PrecachingSpec defines the pre-caching software spec
derived from policies
properties:
excludePrecachePatterns:
items:
type: string
type: array
operatorsIndexes:
items:
type: string
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/ran.openshift.io_clustergroupupgrades.yaml
Expand Up @@ -395,6 +395,10 @@ spec:
description: PrecachingSpec defines the pre-caching software spec
derived from policies
properties:
excludePrecachePatterns:
items:
type: string
type: array
operatorsIndexes:
items:
type: string
Expand Down
4 changes: 4 additions & 0 deletions config/pre-cache/README.md
Expand Up @@ -6,6 +6,7 @@ Supported overrides:
2. `platform.image` - OCP release image
3. `operators.indexes` - OLM index images (list of image pull specs)
4. `operators.packagesAndChannels` - operator packages and channels (list of <package:channel> string entries)
5. `excludePrecachePatterns` - list of patterns to exclude from precaching (using this command: grep -vG -f)

:warning: We need to set the precache.image value as a container image digest. This value is going to be used by the pre-cache task to pull all the required container images and for the upgrade operator to replace the Clusterversion spec.desiredUpdate.image field. The cluster-version operator of the managed cluster requires this format to continue automatically with the upgrade since container image digest uniquely and immutably identifies a container image

Expand Down Expand Up @@ -40,5 +41,8 @@ data:
performance-addon-operator: 4.9
ptp-operator: stable
sriov-network-operator: stable
excludePrecachePatterns: |
aws
thanos
```

6 changes: 5 additions & 1 deletion config/pre-cache/overrides.yaml
Expand Up @@ -13,4 +13,8 @@ data:
# performance-addon-operator: 4.9
# ptp-operator: stable
# sriov-network-operator: stable

# excludePrecachePatterns: |
# aws
# alibaba
# azure
# vsphere
17 changes: 9 additions & 8 deletions controllers/managedClusterResources.go
Expand Up @@ -38,14 +38,15 @@ import (

// templateData provides template rendering data
type templateData struct {
Cluster string
ResourceName string
PlatformImage string
Operators operatorsData
WorkloadImage string
SpaceRequired string
JobTimeout uint64
ViewUpdateIntervalSec int
Cluster string
ResourceName string
PlatformImage string
Operators operatorsData
WorkloadImage string
SpaceRequired string
JobTimeout uint64
ViewUpdateIntervalSec int
ExcludePrecachePatterns []string
}

// operatorsData provides operators data for template rendering
Expand Down
42 changes: 42 additions & 0 deletions controllers/managedClusterResources_test.go
Expand Up @@ -2,6 +2,8 @@ package controllers

import (
"context"
"log"
"os"
"reflect"
"strings"
"testing"
Expand All @@ -25,6 +27,40 @@ func TestMCR_renderYamlTemplates(t *testing.T) {
resourceName string
result string
}{
{
name: "create configmap",
resourceName: "precache-spec",
data: templateData{
Cluster: "test",
ResourceName: "precache-spec",
ExcludePrecachePatterns: []string{"aws", "thanos"},
},
template: templates.MngClusterActCreatePrecachingSpecCM,
result: `
apiVersion: action.open-cluster-management.io/v1beta1
kind: ManagedClusterAction
metadata:
name: precache-spec
namespace: test
spec:
actionType: Create
kube:
resource: configmap
template:
apiVersion: v1
data:
excludePrecachePatterns: |
aws
thanos
operators.indexes: ""
operators.packagesAndChannels: ""
platform.image:
kind: ConfigMap
metadata:
name: pre-cache-spec
namespace: openshift-talo-pre-cache
`,
},
{
name: "create namespace",
resourceName: "test-ns",
Expand Down Expand Up @@ -351,6 +387,12 @@ spec:
t.Errorf("error serializing yaml string: %v", err)
}
assert.Equal(t, true, reflect.DeepEqual(renderedObject, desiredObject))
l := log.New(os.Stderr, "", 0)

if !reflect.DeepEqual(renderedObject, desiredObject) {
l.Println(renderedObject)
l.Println(desiredObject)
}
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions controllers/overrides_test.go
Expand Up @@ -31,12 +31,14 @@ func TestOverrides_getOverrides(t *testing.T) {
"platform.image": "test-platform-image:test-tag",
"operators.indexes": "registry.example.com:5000/test-index:v0.0",
"operators.packagesAndChannels": "local-storage-operator: stable\nperformance-addon-operator: 4.9\nptp-operator: stable\nsriov-network-operator: stable",
"excludePrecachePatterns": "thanos\naws",
},
rdData: map[string]string{
"precache.image": "test-image:test-tag",
"platform.image": "test-platform-image:test-tag",
"operators.indexes": "registry.example.com:5000/test-index:v0.0",
"operators.packagesAndChannels": "local-storage-operator: stable\nperformance-addon-operator: 4.9\nptp-operator: stable\nsriov-network-operator: stable",
"excludePrecachePatterns": "thanos\naws",
},
},
{
Expand All @@ -48,6 +50,7 @@ func TestOverrides_getOverrides(t *testing.T) {
"platform.image": "test-platform-image:test_tag",
"operators.indexes": "registry.example.com:5000/test-index:v0.0",
"operators.packagesAndChannels": "local-storage-operator: stable\nperformance-addon-operator: 4.9\nptp-operator: stable\nsriov-network-operator: stable",
"excludePrecachePatterns": "thanos\naws",
},
rdData: map[string]string{},
},
Expand Down
4 changes: 4 additions & 0 deletions controllers/precache.go
Expand Up @@ -284,6 +284,7 @@ func (r *ClusterGroupUpgradeReconciler) getPrecacheSpecTemplateData(
rv.PlatformImage = spec.PlatformImage
rv.Operators.Indexes = spec.OperatorsIndexes
rv.Operators.PackagesAndChannels = spec.OperatorsPackagesAndChannels
rv.ExcludePrecachePatterns = spec.ExcludePrecachePatterns
return rv
}

Expand All @@ -299,6 +300,7 @@ func (r *ClusterGroupUpgradeReconciler) getPrecacheSpecTemplateData(
// 3. "operators.indexes" - OLM index images (list of index image URIs)
// 4. "operators.packagesAndChannels" - operator packages and channels
// (list of <package:channel> string entries)
// 5. "excludePrecachePatterns" - list of patterns to exclude from precaching
// If overrides are used, the configmap must be created before the CGU
//
// returns: *ranv1alpha1.PrecachingSpec, error
Expand Down Expand Up @@ -333,6 +335,8 @@ func (r *ClusterGroupUpgradeReconciler) includeSoftwareSpecOverrides(
}
rv.OperatorsPackagesAndChannels = operatorsPackagesAndChannels

rv.ExcludePrecachePatterns = strings.Split(overrides["excludePrecachePatterns"], "\n")

if err != nil {
return *rv, err
}
Expand Down
2 changes: 2 additions & 0 deletions controllers/templates/precache-templates.go
Expand Up @@ -55,6 +55,8 @@ spec:
{{ . }} {{ end }}
operators.packagesAndChannels: |{{ range .Operators.PackagesAndChannels }}
{{ . }} {{ end }}
excludePrecachePatterns: |{{ range .ExcludePrecachePatterns }}
{{ . }} {{ end }}
platform.image: {{ .PlatformImage }}
kind: ConfigMap
metadata:
Expand Down
1 change: 1 addition & 0 deletions pre-cache/precache.sh
Expand Up @@ -3,6 +3,7 @@
set -e

rm -rf /host/tmp/precache
rm -f /host/tmp/images.txt
cp -a /opt/precache /host/tmp/
cp -rf /etc/config /host/tmp/precache/config
# only check space for OCP upgrade
Expand Down
12 changes: 11 additions & 1 deletion pre-cache/release
Expand Up @@ -5,7 +5,17 @@ cwd=$(dirname "$0")

extract_pull_spec(){
local rel_img_mount=$1
cat ${rel_img_mount}/release-manifests/image-references |jq '.spec.tags[].from.name' >> $pull_spec_file

# remove empty lines
sed -i '/^[[:space:]]*$/d' $config_volume_path/excludePrecachePatterns
# remove trailing and leading whitespace
sed -i 's/^[ \t]*//;s/[ \t]*$//' $config_volume_path/excludePrecachePatterns

cat ${rel_img_mount}/release-manifests/image-references | \
jq '.spec.tags[] | .name as $name |.from.name as $pull |[$name,$pull] |join("$")' | \
grep -vG -f $config_volume_path/excludePrecachePatterns | \
cut -d "$" -f2 | \
sed 's/^/"/' >> $pull_spec_file
log_debug "Release index image processing done"
}

Expand Down
17 changes: 14 additions & 3 deletions pre-cache/test.sh
Expand Up @@ -21,16 +21,27 @@ echo "test_index1" > /tmp/operators.indexes
echo "test_index2" >> /tmp/operators.indexes
echo " package1: channel1 " > /tmp/operators.packagesAndChannels
echo " package2:channel2" >> /tmp/operators.packagesAndChannels
# the script should ignore traling and leading whitespaces and empty lines
echo -e "\n\n aws \n alibaba \n" > /tmp/excludePrecachePatterns

mkdir -p /tmp/release-manifests
cat <<EOF > /tmp/release-manifests/image-references
{
"spec": {
"tags": [
{
"name": "redhat",
"from": {
"name": "quay.io/1"
}
},
{
"name": "bawsa",
"from": {
"name": "test"
"name": "quay.io/2"
}
}]
}
]
}
}
EOF
Expand Down Expand Up @@ -72,7 +83,7 @@ echo " extract_packages - pass"
echo "Testing release unit:"
result=$(extract_pull_spec "/tmp")
[[ $? -eq 0 ]] || fatal "release_image extract unexpected exit code"
[[ $(cat $pull_spec_file) == "\"test\"" ]] || fatal "release pull spec extract failure"
[[ $(cat $pull_spec_file) == "\"quay.io/1\"" ]] || fatal "release pull spec extract failure"
echo " release extract_pull_spec pass"

# Clean
Expand Down

0 comments on commit 0ce58ae

Please sign in to comment.