discovery-ec2 plugin should check `discovery.type`
As done in #13809 and in Azure, we should check that `discovery.type` is set to `ec2` before starting services. Closes #13581.
This commit is contained in:
parent
6bb7b9b567
commit
314f0749ae
|
@ -20,6 +20,9 @@
|
|||
package org.elasticsearch.cloud.aws;
|
||||
|
||||
import org.elasticsearch.common.inject.AbstractModule;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.discovery.ec2.Ec2Discovery;
|
||||
|
||||
public class Ec2Module extends AbstractModule {
|
||||
|
||||
|
@ -27,4 +30,18 @@ public class Ec2Module extends AbstractModule {
|
|||
protected void configure() {
|
||||
bind(AwsEc2Service.class).to(AwsEc2ServiceImpl.class).asEagerSingleton();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if discovery is meant to start
|
||||
* @return true if we can start discovery features
|
||||
*/
|
||||
public static boolean isEc2DiscoveryActive(Settings settings, ESLogger logger) {
|
||||
// User set discovery.type: ec2
|
||||
if (!Ec2Discovery.EC2.equalsIgnoreCase(settings.get("discovery.type"))) {
|
||||
logger.trace("discovery.type not set to {}", Ec2Discovery.EC2);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,8 +21,6 @@ package org.elasticsearch.discovery.ec2;
|
|||
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.cluster.ClusterService;
|
||||
import org.elasticsearch.cluster.settings.ClusterDynamicSettings;
|
||||
import org.elasticsearch.cluster.settings.DynamicSettings;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.discovery.DiscoverySettings;
|
||||
|
@ -38,6 +36,8 @@ import org.elasticsearch.transport.TransportService;
|
|||
*/
|
||||
public class Ec2Discovery extends ZenDiscovery {
|
||||
|
||||
public static final String EC2 = "ec2";
|
||||
|
||||
@Inject
|
||||
public Ec2Discovery(Settings settings, ClusterName clusterName, ThreadPool threadPool, TransportService transportService,
|
||||
ClusterService clusterService, NodeSettingsService nodeSettingsService, ZenPingService pingService,
|
||||
|
|
|
@ -23,6 +23,9 @@ import org.elasticsearch.cloud.aws.AwsEc2ServiceImpl;
|
|||
import org.elasticsearch.cloud.aws.Ec2Module;
|
||||
import org.elasticsearch.common.component.LifecycleComponent;
|
||||
import org.elasticsearch.common.inject.Module;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.discovery.DiscoveryModule;
|
||||
import org.elasticsearch.discovery.ec2.AwsEc2UnicastHostsProvider;
|
||||
import org.elasticsearch.discovery.ec2.Ec2Discovery;
|
||||
|
@ -55,6 +58,13 @@ public class Ec2DiscoveryPlugin extends Plugin {
|
|||
});
|
||||
}
|
||||
|
||||
private final Settings settings;
|
||||
protected final ESLogger logger = Loggers.getLogger(Ec2DiscoveryPlugin.class);
|
||||
|
||||
public Ec2DiscoveryPlugin(Settings settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "discovery-ec2";
|
||||
|
@ -80,7 +90,9 @@ public class Ec2DiscoveryPlugin extends Plugin {
|
|||
}
|
||||
|
||||
public void onModule(DiscoveryModule discoveryModule) {
|
||||
discoveryModule.addDiscoveryType("ec2", Ec2Discovery.class);
|
||||
discoveryModule.addUnicastHostProvider(AwsEc2UnicastHostsProvider.class);
|
||||
if (Ec2Module.isEc2DiscoveryActive(settings, logger)) {
|
||||
discoveryModule.addDiscoveryType("ec2", Ec2Discovery.class);
|
||||
discoveryModule.addUnicastHostProvider(AwsEc2UnicastHostsProvider.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.discovery.ec2;
|
||||
|
||||
import org.elasticsearch.cloud.aws.Ec2Module;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class Ec2DiscoverySettingsTests extends ESTestCase {
|
||||
|
||||
public void testDiscoveryReady() {
|
||||
Settings settings = Settings.builder()
|
||||
.put("discovery.type", "ec2")
|
||||
.build();
|
||||
boolean discoveryReady = Ec2Module.isEc2DiscoveryActive(settings, logger);
|
||||
assertThat(discoveryReady, is(true));
|
||||
}
|
||||
|
||||
public void testDiscoveryNotReady() {
|
||||
Settings settings = Settings.EMPTY;
|
||||
boolean discoveryReady = Ec2Module.isEc2DiscoveryActive(settings, logger);
|
||||
assertThat(discoveryReady, is(false));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue