Skip to content

fix(jenkins): fix the issue of Retrofit2EncodeCorrectionInterceptor getting added to Jenkins retrofit1 client #1324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule
import com.jakewharton.retrofit.Ok3Client
import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.config.OkHttp3ClientConfiguration
import com.netflix.spinnaker.fiat.model.resources.Permissions
import com.netflix.spinnaker.igor.IgorConfigurationProperties
import com.netflix.spinnaker.igor.config.client.DefaultJenkinsOkHttpClientProvider
Expand Down Expand Up @@ -69,6 +70,11 @@ import java.util.concurrent.TimeUnit
@EnableConfigurationProperties(JenkinsProperties)
class JenkinsConfig {

@Bean
OkHttpClient okHttpClient(OkHttp3ClientConfiguration okHttpClientConfig) {
return okHttpClientConfig.create().build();
}

@Bean
@ConditionalOnMissingBean
JenkinsOkHttpClientProvider jenkinsOkHttpClientProvider(OkHttpClient okHttpClient) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2025 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.igor

import com.netflix.spectator.api.NoopRegistry
import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.igor.config.JenkinsConfig
import com.netflix.spinnaker.igor.config.client.JenkinsOkHttpClientProvider
import com.netflix.spinnaker.igor.service.BuildServices
import com.netflix.spinnaker.okhttp.Retrofit2EncodeCorrectionInterceptor
import com.netflix.spinnaker.config.OkHttp3ClientConfiguration
import com.netflix.spinnaker.config.okhttp3.RawOkHttpClientConfiguration;
import com.netflix.spinnaker.igor.config.JenkinsProperties
import com.netflix.spinnaker.config.OkHttpClientComponents
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry
import io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Bean;
import retrofit.RestAdapter
import spock.lang.Specification;

@SpringBootTest(classes = [JenkinsConfig, RawOkHttpClientConfiguration, OkHttpClientComponents, TestConfiguration, OkHttp3ClientConfiguration],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is RawOkHttpClientConfiguration still required after the fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not needed but it shows the real scenario of two OkHttpClient beans available but JenkinsConfig is using the right one. Let me know if you want me remove it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm...this is a tough one. Aren't we getting lucky here? RawOkHttpClientConfiguration uses @ConditionalOnMissingBean. What guarantees do we have that JenkinsConfig doesn't pick that up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed RawOkHttpClientConfiguration and converted the test to java.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. What about @ConditionalOnMissingBean though? Aren't we still getting lucky in the main igor code, because Main has:

@SpringBootApplication(
  scanBasePackages = [
    "",
    "com.netflix.spinnaker.igor"
  ],
  exclude = [GroovyTemplateAutoConfiguration]
)

and specifically com.netflix.spinnaker.config because RawOkHttpClientConfiguration is in

package com.netflix.spinnaker.config.okhttp3;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If RawOkHttpClientConfiguration gets processed first, I guess we end up with two OkHttpClient beans, one named okhttp3Client (from RawOkHttpClientConfiguration) and one named okHttpClient (from JenkinsConfig). And since the rest of the code in JenkinsConfig uses the variable name okHttpClient (and not okhttp3Client) perhaps things "just work." We're gonna go ahead ...

properties = ["jenkins.enabled=true"])
class JenkinsClientSpec extends Specification {

@Autowired
JenkinsOkHttpClientProvider jenkinsOkHttpClientProvider

@Autowired
JenkinsProperties.JenkinsHost jenkinsHost

def "test if jenkins retrofit1 client has retrofit2 interceptor - Retrofit2EncodeCorrectionInterceptor"() {
when:
def client = jenkinsOkHttpClientProvider.provide(jenkinsHost)
def interceptors = client.interceptors()

then:
interceptors.size() != 0
!interceptors.any { it instanceof Retrofit2EncodeCorrectionInterceptor }
}
}

class TestConfiguration {
@Bean
TaskExecutorBuilder taskExecutorBuilder() {
return new TaskExecutorBuilder()
}

@Bean
JenkinsProperties.JenkinsHost jenkinsHost() {
def host = new JenkinsProperties.JenkinsHost()
host.address = "http://localhost:8080"
host.name = "jenkins-dev"
return host
}

@Bean
JenkinsProperties jenkinsProperties(JenkinsProperties.JenkinsHost jenkinsHost) {
def jenkinsProperties = new JenkinsProperties()
jenkinsProperties.masters = [jenkinsHost]
return jenkinsProperties
}

@Bean
BuildServices buildServices() {
def buildServices = new BuildServices()
return buildServices
}

@Bean
IgorConfigurationProperties igorConfigurationProperties() {
new IgorConfigurationProperties()
}

@Bean
Registry registry() {
new NoopRegistry()
}

@Bean
CircuitBreakerRegistry circuitBreakerRegistry() {
new InMemoryCircuitBreakerRegistry()
}

@Bean
RestAdapter.LogLevel retrofitLogLevel() {
return RestAdapter.LogLevel.BASIC
}

@Bean
HttpLoggingInterceptor.Level retrofitInterceptorLogLevel() {
return HttpLoggingInterceptor.Level.BODY
}
}