github.com/khulnasoft-lab/khulnasoft@v26.0.1-0.20240328202558-330a6f959fe0+incompatible/image/cache/compare.go (about) 1 package cache // import "github.com/docker/docker/image/cache" 2 3 import ( 4 "strings" 5 6 "github.com/containerd/containerd/platforms" 7 "github.com/docker/docker/api/types/container" 8 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 9 ) 10 11 func comparePlatform(builderPlatform, imagePlatform ocispec.Platform) bool { 12 // On Windows, only check the Major and Minor versions. 13 // The Build and Revision compatibility depends on whether `process` or 14 // `hyperv` isolation used. 15 // 16 // Fixes https://github.com/moby/moby/issues/47307 17 if builderPlatform.OS == "windows" && imagePlatform.OS == builderPlatform.OS { 18 // OSVersion format is: 19 // Major.Minor.Build.Revision 20 builderParts := strings.Split(builderPlatform.OSVersion, ".") 21 imageParts := strings.Split(imagePlatform.OSVersion, ".") 22 23 if len(builderParts) >= 3 && len(imageParts) >= 3 { 24 // Keep only Major & Minor. 25 builderParts[0] = imageParts[0] 26 builderParts[1] = imageParts[1] 27 imagePlatform.OSVersion = strings.Join(builderParts, ".") 28 } 29 } 30 31 return platforms.Only(builderPlatform).Match(imagePlatform) 32 } 33 34 // compare two Config struct. Do not container-specific fields: 35 // - Image 36 // - Hostname 37 // - Domainname 38 // - MacAddress 39 func compare(a, b *container.Config) bool { 40 if a == nil || b == nil { 41 return false 42 } 43 44 if len(a.Env) != len(b.Env) { 45 return false 46 } 47 if len(a.Cmd) != len(b.Cmd) { 48 return false 49 } 50 if len(a.Entrypoint) != len(b.Entrypoint) { 51 return false 52 } 53 if len(a.Shell) != len(b.Shell) { 54 return false 55 } 56 if len(a.ExposedPorts) != len(b.ExposedPorts) { 57 return false 58 } 59 if len(a.Volumes) != len(b.Volumes) { 60 return false 61 } 62 if len(a.Labels) != len(b.Labels) { 63 return false 64 } 65 if len(a.OnBuild) != len(b.OnBuild) { 66 return false 67 } 68 69 for i := 0; i < len(a.Env); i++ { 70 if a.Env[i] != b.Env[i] { 71 return false 72 } 73 } 74 for i := 0; i < len(a.OnBuild); i++ { 75 if a.OnBuild[i] != b.OnBuild[i] { 76 return false 77 } 78 } 79 for i := 0; i < len(a.Cmd); i++ { 80 if a.Cmd[i] != b.Cmd[i] { 81 return false 82 } 83 } 84 for i := 0; i < len(a.Entrypoint); i++ { 85 if a.Entrypoint[i] != b.Entrypoint[i] { 86 return false 87 } 88 } 89 for i := 0; i < len(a.Shell); i++ { 90 if a.Shell[i] != b.Shell[i] { 91 return false 92 } 93 } 94 for k := range a.ExposedPorts { 95 if _, exists := b.ExposedPorts[k]; !exists { 96 return false 97 } 98 } 99 for key := range a.Volumes { 100 if _, exists := b.Volumes[key]; !exists { 101 return false 102 } 103 } 104 for k, v := range a.Labels { 105 if v != b.Labels[k] { 106 return false 107 } 108 } 109 110 if a.AttachStdin != b.AttachStdin { 111 return false 112 } 113 if a.AttachStdout != b.AttachStdout { 114 return false 115 } 116 if a.AttachStderr != b.AttachStderr { 117 return false 118 } 119 if a.NetworkDisabled != b.NetworkDisabled { 120 return false 121 } 122 if a.Tty != b.Tty { 123 return false 124 } 125 if a.OpenStdin != b.OpenStdin { 126 return false 127 } 128 if a.StdinOnce != b.StdinOnce { 129 return false 130 } 131 if a.ArgsEscaped != b.ArgsEscaped { 132 return false 133 } 134 if a.User != b.User { 135 return false 136 } 137 if a.WorkingDir != b.WorkingDir { 138 return false 139 } 140 if a.StopSignal != b.StopSignal { 141 return false 142 } 143 144 if (a.StopTimeout == nil) != (b.StopTimeout == nil) { 145 return false 146 } 147 if a.StopTimeout != nil && b.StopTimeout != nil { 148 if *a.StopTimeout != *b.StopTimeout { 149 return false 150 } 151 } 152 if (a.Healthcheck == nil) != (b.Healthcheck == nil) { 153 return false 154 } 155 if a.Healthcheck != nil && b.Healthcheck != nil { 156 if a.Healthcheck.Interval != b.Healthcheck.Interval { 157 return false 158 } 159 if a.Healthcheck.StartInterval != b.Healthcheck.StartInterval { 160 return false 161 } 162 if a.Healthcheck.StartPeriod != b.Healthcheck.StartPeriod { 163 return false 164 } 165 if a.Healthcheck.Timeout != b.Healthcheck.Timeout { 166 return false 167 } 168 if a.Healthcheck.Retries != b.Healthcheck.Retries { 169 return false 170 } 171 if len(a.Healthcheck.Test) != len(b.Healthcheck.Test) { 172 return false 173 } 174 for i := 0; i < len(a.Healthcheck.Test); i++ { 175 if a.Healthcheck.Test[i] != b.Healthcheck.Test[i] { 176 return false 177 } 178 } 179 } 180 181 return true 182 }