|
1 | 1 | package imagepolicy
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "os"
|
5 | 6 | "reflect"
|
6 | 7 | "strings"
|
@@ -436,9 +437,16 @@ func TestAdmissionResolveImages(t *testing.T) {
|
436 | 437 | DockerImageReference: "integrated.registry/image1/image1:latest",
|
437 | 438 | }
|
438 | 439 |
|
| 440 | + obj, err := configlatest.ReadYAML(bytes.NewBufferString(`{"kind":"ImagePolicyConfig","apiVersion":"v1"}`)) |
| 441 | + if err != nil || obj == nil { |
| 442 | + t.Fatal(err) |
| 443 | + } |
| 444 | + defaultPolicyConfig := obj.(*api.ImagePolicyConfig) |
| 445 | + |
439 | 446 | testCases := []struct {
|
440 | 447 | client *testclient.Fake
|
441 | 448 | policy api.ImageResolutionType
|
| 449 | + config *api.ImagePolicyConfig |
442 | 450 | attrs admission.Attributes
|
443 | 451 | admit bool
|
444 | 452 | expect runtime.Object
|
@@ -585,6 +593,168 @@ func TestAdmissionResolveImages(t *testing.T) {
|
585 | 593 | },
|
586 | 594 | },
|
587 | 595 | },
|
| 596 | + |
| 597 | + // resolves images in the integrated registry on builds without altering their ref (avoids looking up the tag) |
| 598 | + { |
| 599 | + policy: api.RequiredRewrite, |
| 600 | + client: testclient.NewSimpleFake( |
| 601 | + image1, |
| 602 | + ), |
| 603 | + attrs: admission.NewAttributesRecord( |
| 604 | + &buildapi.Build{ |
| 605 | + Spec: buildapi.BuildSpec{ |
| 606 | + CommonSpec: buildapi.CommonSpec{ |
| 607 | + Strategy: buildapi.BuildStrategy{ |
| 608 | + SourceStrategy: &buildapi.SourceBuildStrategy{ |
| 609 | + From: kapi.ObjectReference{Kind: "DockerImage", Name: "integrated.registry/test/mysql@sha256:0000000000000000000000000000000000000000000000000000000000000001"}, |
| 610 | + }, |
| 611 | + }, |
| 612 | + }, |
| 613 | + }, |
| 614 | + }, nil, schema.GroupVersionKind{Version: "v1", Kind: "Build"}, |
| 615 | + "default", "build1", schema.GroupVersionResource{Version: "v1", Resource: "builds"}, |
| 616 | + "", admission.Create, nil, |
| 617 | + ), |
| 618 | + admit: true, |
| 619 | + expect: &buildapi.Build{ |
| 620 | + Spec: buildapi.BuildSpec{ |
| 621 | + CommonSpec: buildapi.CommonSpec{ |
| 622 | + Strategy: buildapi.BuildStrategy{ |
| 623 | + SourceStrategy: &buildapi.SourceBuildStrategy{ |
| 624 | + From: kapi.ObjectReference{Kind: "DockerImage", Name: "integrated.registry/test/mysql@sha256:0000000000000000000000000000000000000000000000000000000000000001"}, |
| 625 | + }, |
| 626 | + }, |
| 627 | + }, |
| 628 | + }, |
| 629 | + }, |
| 630 | + }, |
| 631 | + // does not rewrite the config because build has DoNotAttempt by default, which overrides global policy |
| 632 | + { |
| 633 | + config: &api.ImagePolicyConfig{ |
| 634 | + ResolveImages: api.RequiredRewrite, |
| 635 | + ResolutionRules: []api.ImageResolutionPolicyRule{ |
| 636 | + {TargetResource: metav1.GroupResource{Group: "", Resource: "builds"}}, |
| 637 | + }, |
| 638 | + }, |
| 639 | + client: testclient.NewSimpleFake( |
| 640 | + &imageapi.ImageStreamTag{ |
| 641 | + ObjectMeta: metav1.ObjectMeta{Name: "test:other", Namespace: "default"}, |
| 642 | + Image: *image1, |
| 643 | + }, |
| 644 | + ), |
| 645 | + attrs: admission.NewAttributesRecord( |
| 646 | + &buildapi.Build{ |
| 647 | + Spec: buildapi.BuildSpec{ |
| 648 | + CommonSpec: buildapi.CommonSpec{ |
| 649 | + Strategy: buildapi.BuildStrategy{ |
| 650 | + CustomStrategy: &buildapi.CustomBuildStrategy{ |
| 651 | + From: kapi.ObjectReference{Kind: "ImageStreamTag", Name: "test:other"}, |
| 652 | + }, |
| 653 | + }, |
| 654 | + }, |
| 655 | + }, |
| 656 | + }, nil, schema.GroupVersionKind{Version: "v1", Kind: "Build"}, |
| 657 | + "default", "build1", schema.GroupVersionResource{Version: "v1", Resource: "builds"}, |
| 658 | + "", admission.Create, nil, |
| 659 | + ), |
| 660 | + admit: true, |
| 661 | + expect: &buildapi.Build{ |
| 662 | + Spec: buildapi.BuildSpec{ |
| 663 | + CommonSpec: buildapi.CommonSpec{ |
| 664 | + Strategy: buildapi.BuildStrategy{ |
| 665 | + CustomStrategy: &buildapi.CustomBuildStrategy{ |
| 666 | + From: kapi.ObjectReference{Kind: "ImageStreamTag", Name: "test:other"}, |
| 667 | + }, |
| 668 | + }, |
| 669 | + }, |
| 670 | + }, |
| 671 | + }, |
| 672 | + }, |
| 673 | + // does not rewrite the config because build has Attempt by default, which overrides global policy |
| 674 | + { |
| 675 | + config: &api.ImagePolicyConfig{ |
| 676 | + ResolveImages: api.RequiredRewrite, |
| 677 | + ResolutionRules: []api.ImageResolutionPolicyRule{ |
| 678 | + {TargetResource: metav1.GroupResource{Group: "", Resource: "builds"}, Policy: api.Attempt}, |
| 679 | + }, |
| 680 | + }, |
| 681 | + client: testclient.NewSimpleFake( |
| 682 | + &imageapi.ImageStreamTag{ |
| 683 | + ObjectMeta: metav1.ObjectMeta{Name: "test:other", Namespace: "default"}, |
| 684 | + Image: *image1, |
| 685 | + }, |
| 686 | + ), |
| 687 | + attrs: admission.NewAttributesRecord( |
| 688 | + &buildapi.Build{ |
| 689 | + Spec: buildapi.BuildSpec{ |
| 690 | + CommonSpec: buildapi.CommonSpec{ |
| 691 | + Strategy: buildapi.BuildStrategy{ |
| 692 | + CustomStrategy: &buildapi.CustomBuildStrategy{ |
| 693 | + From: kapi.ObjectReference{Kind: "ImageStreamTag", Name: "test:other"}, |
| 694 | + }, |
| 695 | + }, |
| 696 | + }, |
| 697 | + }, |
| 698 | + }, nil, schema.GroupVersionKind{Version: "v1", Kind: "Build"}, |
| 699 | + "default", "build1", schema.GroupVersionResource{Version: "v1", Resource: "builds"}, |
| 700 | + "", admission.Create, nil, |
| 701 | + ), |
| 702 | + admit: true, |
| 703 | + expect: &buildapi.Build{ |
| 704 | + Spec: buildapi.BuildSpec{ |
| 705 | + CommonSpec: buildapi.CommonSpec{ |
| 706 | + Strategy: buildapi.BuildStrategy{ |
| 707 | + CustomStrategy: &buildapi.CustomBuildStrategy{ |
| 708 | + From: kapi.ObjectReference{Kind: "ImageStreamTag", Name: "test:other"}, |
| 709 | + }, |
| 710 | + }, |
| 711 | + }, |
| 712 | + }, |
| 713 | + }, |
| 714 | + }, |
| 715 | + // rewrites the config because build has AttemptRewrite which overrides the global policy |
| 716 | + { |
| 717 | + config: &api.ImagePolicyConfig{ |
| 718 | + ResolveImages: api.DoNotAttempt, |
| 719 | + ResolutionRules: []api.ImageResolutionPolicyRule{ |
| 720 | + {TargetResource: metav1.GroupResource{Group: "", Resource: "builds"}, Policy: api.AttemptRewrite}, |
| 721 | + }, |
| 722 | + }, |
| 723 | + client: testclient.NewSimpleFake( |
| 724 | + &imageapi.ImageStreamTag{ |
| 725 | + ObjectMeta: metav1.ObjectMeta{Name: "test:other", Namespace: "default"}, |
| 726 | + Image: *image1, |
| 727 | + }, |
| 728 | + ), |
| 729 | + attrs: admission.NewAttributesRecord( |
| 730 | + &buildapi.Build{ |
| 731 | + Spec: buildapi.BuildSpec{ |
| 732 | + CommonSpec: buildapi.CommonSpec{ |
| 733 | + Strategy: buildapi.BuildStrategy{ |
| 734 | + CustomStrategy: &buildapi.CustomBuildStrategy{ |
| 735 | + From: kapi.ObjectReference{Kind: "ImageStreamTag", Name: "test:other"}, |
| 736 | + }, |
| 737 | + }, |
| 738 | + }, |
| 739 | + }, |
| 740 | + }, nil, schema.GroupVersionKind{Version: "v1", Kind: "Build"}, |
| 741 | + "default", "build1", schema.GroupVersionResource{Version: "v1", Resource: "builds"}, |
| 742 | + "", admission.Create, nil, |
| 743 | + ), |
| 744 | + admit: true, |
| 745 | + expect: &buildapi.Build{ |
| 746 | + Spec: buildapi.BuildSpec{ |
| 747 | + CommonSpec: buildapi.CommonSpec{ |
| 748 | + Strategy: buildapi.BuildStrategy{ |
| 749 | + CustomStrategy: &buildapi.CustomBuildStrategy{ |
| 750 | + From: kapi.ObjectReference{Kind: "DockerImage", Name: "integrated.registry/image1/image1@sha256:0000000000000000000000000000000000000000000000000000000000000001"}, |
| 751 | + }, |
| 752 | + }, |
| 753 | + }, |
| 754 | + }, |
| 755 | + }, |
| 756 | + }, |
| 757 | + |
588 | 758 | // resolves builds.build.openshift.io with image stream tags, uses the image DockerImageReference with SHA set.
|
589 | 759 | {
|
590 | 760 | policy: api.RequiredRewrite,
|
@@ -735,6 +905,43 @@ func TestAdmissionResolveImages(t *testing.T) {
|
735 | 905 | },
|
736 | 906 | },
|
737 | 907 | },
|
| 908 | + // does not resolve replica sets by default |
| 909 | + { |
| 910 | + config: defaultPolicyConfig, |
| 911 | + client: testclient.NewSimpleFake( |
| 912 | + &imageapi.ImageStreamTag{ |
| 913 | + ObjectMeta: metav1.ObjectMeta{Name: "test:other", Namespace: "default"}, |
| 914 | + Image: *image1, |
| 915 | + }, |
| 916 | + ), |
| 917 | + attrs: admission.NewAttributesRecord( |
| 918 | + &kapiextensions.ReplicaSet{ |
| 919 | + Spec: kapiextensions.ReplicaSetSpec{ |
| 920 | + Template: kapi.PodTemplateSpec{ |
| 921 | + Spec: kapi.PodSpec{ |
| 922 | + Containers: []kapi.Container{ |
| 923 | + {Image: "integrated.registry/default/test:other"}, |
| 924 | + }, |
| 925 | + }, |
| 926 | + }, |
| 927 | + }, |
| 928 | + }, nil, schema.GroupVersionKind{Version: "v1", Kind: "ReplicaSet", Group: "extensions"}, |
| 929 | + "default", "rs1", schema.GroupVersionResource{Version: "v1", Resource: "replicasets", Group: "extensions"}, |
| 930 | + "", admission.Create, nil, |
| 931 | + ), |
| 932 | + admit: true, |
| 933 | + expect: &kapiextensions.ReplicaSet{ |
| 934 | + Spec: kapiextensions.ReplicaSetSpec{ |
| 935 | + Template: kapi.PodTemplateSpec{ |
| 936 | + Spec: kapi.PodSpec{ |
| 937 | + Containers: []kapi.Container{ |
| 938 | + {Image: "integrated.registry/default/test:other"}, |
| 939 | + }, |
| 940 | + }, |
| 941 | + }, |
| 942 | + }, |
| 943 | + }, |
| 944 | + }, |
738 | 945 | // resolves replica sets that specifically request lookup
|
739 | 946 | {
|
740 | 947 | policy: api.RequiredRewrite,
|
@@ -925,16 +1132,21 @@ func TestAdmissionResolveImages(t *testing.T) {
|
925 | 1132 | }
|
926 | 1133 | for i, test := range testCases {
|
927 | 1134 | onResources := []schema.GroupResource{{Resource: "builds"}, {Resource: "pods"}}
|
928 |
| - p, err := newImagePolicyPlugin(&api.ImagePolicyConfig{ |
929 |
| - ResolveImages: test.policy, |
930 |
| - ResolutionRules: []api.ImageResolutionPolicyRule{ |
931 |
| - {LocalNames: true, TargetResource: metav1.GroupResource{Resource: "*"}}, |
932 |
| - {LocalNames: true, TargetResource: metav1.GroupResource{Group: "extensions", Resource: "*"}}, |
933 |
| - }, |
934 |
| - ExecutionRules: []api.ImageExecutionPolicyRule{ |
935 |
| - {ImageCondition: api.ImageCondition{OnResources: onResources}}, |
936 |
| - }, |
937 |
| - }) |
| 1135 | + config := test.config |
| 1136 | + if config == nil { |
| 1137 | + // old style config |
| 1138 | + config = &api.ImagePolicyConfig{ |
| 1139 | + ResolveImages: test.policy, |
| 1140 | + ResolutionRules: []api.ImageResolutionPolicyRule{ |
| 1141 | + {LocalNames: true, TargetResource: metav1.GroupResource{Resource: "*"}, Policy: test.policy}, |
| 1142 | + {LocalNames: true, TargetResource: metav1.GroupResource{Group: "extensions", Resource: "*"}, Policy: test.policy}, |
| 1143 | + }, |
| 1144 | + ExecutionRules: []api.ImageExecutionPolicyRule{ |
| 1145 | + {ImageCondition: api.ImageCondition{OnResources: onResources}}, |
| 1146 | + }, |
| 1147 | + } |
| 1148 | + } |
| 1149 | + p, err := newImagePolicyPlugin(config) |
938 | 1150 | if err != nil {
|
939 | 1151 | t.Fatal(err)
|
940 | 1152 | }
|
|
0 commit comments