Search in sources :

Example 41 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class AutoScaleController method groupEvent.

@Override
public void groupEvent(Group<AutoScalerNode> group, GroupEvent event) {
    DataStore dataStore = fabricService.get().adapt(DataStore.class);
    switch(event) {
        case CONNECTED:
        case CHANGED:
            if (isValid()) {
                AutoScalerNode state = createState();
                try {
                    if (group.isMaster()) {
                        enableMasterZkCache(curator.get());
                        LOGGER.info("AutoScaleController is the master");
                        group.update(state);
                        dataStore.trackConfiguration(runnable);
                        enableTimer();
                        onConfigurationChanged();
                    } else {
                        LOGGER.info("AutoScaleController is not the master");
                        group.update(state);
                        disableTimer();
                        dataStore.untrackConfiguration(runnable);
                        disableMasterZkCache();
                    }
                } catch (IllegalStateException e) {
                // Ignore
                }
            } else {
                LOGGER.info("Not valid with master: " + group.isMaster() + " fabric: " + fabricService.get() + " curator: " + curator.get());
            }
            break;
        case DISCONNECTED:
            dataStore.untrackConfiguration(runnable);
    }
}
Also used : DataStore(io.fabric8.api.DataStore)

Example 42 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class MQServiceImpl method createOrUpdateMQProfile.

@Override
public Profile createOrUpdateMQProfile(String versionId, String profileId, String brokerName, Map<String, String> configs, boolean replicated) {
    Version version = profileService.getRequiredVersion(versionId);
    String parentProfileName = null;
    if (configs != null && configs.containsKey("parent")) {
        parentProfileName = configs.remove("parent");
    }
    if (Strings.isNullOrBlank(parentProfileName)) {
        parentProfileName = replicated ? MQ_PROFILE_REPLICATED : MQ_PROFILE_BASE;
    }
    Profile parentProfile = version.getRequiredProfile(parentProfileName);
    if (brokerName == null || profileId == null) {
        return parentProfile;
    }
    String pidName = getBrokerPID(brokerName);
    // lets check we have a config value
    ProfileBuilder builder;
    Profile overlay;
    // create a profile if it doesn't exist
    Map<String, String> config = null;
    boolean create = !version.hasProfile(profileId);
    if (create) {
        builder = ProfileBuilder.Factory.create(versionId, profileId);
        if (parentProfile != null) {
            builder.addParent(parentProfile.getId());
        }
        overlay = profileService.getOverlayProfile(parentProfile);
    } else {
        Profile profile = version.getRequiredProfile(profileId);
        builder = ProfileBuilder.Factory.createFrom(profile);
        config = builder.getConfiguration(pidName);
        overlay = profileService.getOverlayProfile(profile);
    }
    Map<String, String> parentProfileConfig = ProfileBuilder.Factory.createFrom(overlay).getConfiguration(MQ_PID_TEMPLATE);
    if (config == null) {
        config = parentProfileConfig;
    }
    if (configs != null && "true".equals(configs.get("ssl"))) {
        // Only generate the keystore file if it does not exist.
        // [TOOD] Fix direct data access! This should be part of the ProfileBuilder
        byte[] keystore = overlay.getFileConfiguration("keystore.jks");
        if (keystore == null) {
            try {
                String host = configs.get("keystore.cn");
                if (host == null) {
                    host = configs.get(GROUP);
                    if (host == null) {
                        host = "localhost";
                    }
                    configs.put("keystore.cn", host);
                }
                String password = configs.get("keystore.password");
                if (password == null) {
                    password = generatePassword(8);
                    configs.put("keystore.password", password);
                }
                File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                keystoreFile.delete();
                LOG.info("Generating ssl keystore...");
                int rc = system("keytool", "-genkey", "-storetype", "JKS", "-storepass", password, "-keystore", keystoreFile.getCanonicalPath(), "-keypass", password, "-alias", host, "-keyalg", "RSA", "-keysize", "4096", "-dname", String.format("cn=%s", host), "-validity", "3650");
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                keystore = Files.readBytes(keystoreFile);
                keystoreFile.delete();
                LOG.info("Keystore generated");
                builder.addFileConfiguration("keystore.jks", keystore);
                configs.put("keystore.file", "profile:keystore.jks");
            } catch (IOException e) {
                LOG.error("Failed to generate keystore.jks: " + e.getMessage(), e);
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        // [TOOD] Fix direct data access! This should be part of the ProfileBuilder
        byte[] truststore = overlay.getFileConfiguration("truststore.jks");
        if (truststore == null && configs.get("keystore.password") != null) {
            try {
                String password = configs.get("truststore.password");
                if (password == null) {
                    password = configs.get("keystore.password");
                    configs.put("truststore.password", password);
                }
                File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                Files.writeToFile(keystoreFile, keystore);
                File certFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                certFile.delete();
                LOG.info("Exporting broker certificate to create truststore.jks");
                int rc = system("keytool", "-exportcert", "-rfc", "-keystore", keystoreFile.getCanonicalPath(), "-storepass", configs.get("keystore.password"), "-alias", configs.get("keystore.cn"), "--file", certFile.getCanonicalPath());
                keystoreFile.delete();
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                LOG.info("Creating truststore.jks");
                File truststoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                truststoreFile.delete();
                rc = system("keytool", "-importcert", "-noprompt", "-keystore", truststoreFile.getCanonicalPath(), "-storepass", password, "--file", certFile.getCanonicalPath());
                certFile.delete();
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                truststore = Files.readBytes(truststoreFile);
                truststoreFile.delete();
                builder.addFileConfiguration("truststore.jks", truststore);
                configs.put("truststore.file", "profile:truststore.jks");
            } catch (IOException e) {
                LOG.error("Failed to generate truststore.jks due: " + e.getMessage(), e);
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
    config.put("broker-name", brokerName);
    if (configs != null) {
        config.putAll(configs);
    }
    // lets check we've a bunch of config values inherited from the template
    String[] propertiesToDefault = { CONFIG_URL, STANDBY_POOL, CONNECTORS };
    for (String key : propertiesToDefault) {
        if (config.get(key) == null) {
            String defaultValue = parentProfileConfig.get(key);
            if (Strings.isNotBlank(defaultValue)) {
                config.put(key, defaultValue);
            }
        }
    }
    // config map is not from "official" profile, so it doesn't have to use felix' Properties class
    builder.addConfiguration(pidName, config);
    Profile profile = builder.getProfile();
    return create ? profileService.createProfile(profile) : profileService.updateProfile(profile);
}
Also used : Version(io.fabric8.api.Version) IOException(java.io.IOException) ProfileBuilder(io.fabric8.api.ProfileBuilder) File(java.io.File) Profile(io.fabric8.api.Profile)

Example 43 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class MQManager method createConfigDTOs.

public static List<MQBrokerConfigDTO> createConfigDTOs(MQService mqService, Profile profile) {
    List<MQBrokerConfigDTO> answer = new ArrayList<MQBrokerConfigDTO>();
    Map<String, Map<String, String>> configurations = profile.getConfigurations();
    Set<Map.Entry<String, Map<String, String>>> entries = configurations.entrySet();
    for (Map.Entry<String, Map<String, String>> entry : entries) {
        String key = entry.getKey();
        Map<String, String> configuration = entry.getValue();
        if (isBrokerConfigPid(key)) {
            String brokerName = getBrokerNameFromPID(key);
            String profileId = profile.getId();
            MQBrokerConfigDTO dto = new MQBrokerConfigDTO();
            dto.setProfile(profileId);
            dto.setBrokerName(brokerName);
            String version = profile.getVersion();
            dto.setVersion(version);
            List<String> parentIds = profile.getParentIds();
            if (parentIds.size() > 0) {
                dto.setParentProfile(parentIds.get(0));
            }
            if (configuration != null) {
                dto.setData(configuration.get(DATA));
                dto.setConfigUrl(configuration.get(CONFIG_URL));
                dto.setGroup(configuration.get(GROUP));
                dto.setKind(BrokerKind.fromValue(configuration.get(KIND)));
                dto.setMinimumInstances(Maps.integerValue(configuration, MINIMUM_INSTANCES));
                dto.setNetworks(Maps.stringValues(configuration, NETWORKS));
                dto.setNetworksUserName(configuration.get(NETWORK_USER_NAME));
                dto.setNetworksPassword(configuration.get(NETWORK_PASSWORD));
                dto.setReplicas(Maps.integerValue(configuration, REPLICAS));
                for (Map.Entry<String, String> configurationEntry : configuration.entrySet()) {
                    if (configurationEntry.getKey().endsWith("-port")) {
                        dto.getPorts().put(configurationEntry.getKey().substring(0, configurationEntry.getKey().indexOf("-port")), configurationEntry.getValue());
                    }
                }
            }
            answer.add(dto);
        }
    }
    return answer;
}
Also used : MQBrokerConfigDTO(io.fabric8.api.jmx.MQBrokerConfigDTO) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 44 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class ContainerCreateCloud method doExecute.

@Override
protected Object doExecute() throws Exception {
    // validate input before creating containers
    preCreateContainer(name);
    FabricValidations.validateProfileNames(profiles);
    if (isEnsembleServer && newUserPassword == null) {
        newUserPassword = zookeeperPassword != null ? zookeeperPassword : fabricService.getZookeeperPassword();
    }
    CreateJCloudsContainerOptions.Builder builder = CreateJCloudsContainerOptions.builder().name(name).bindAddress(bindAddress).resolver(resolver).manualIp(manualIp).ensembleServer(isEnsembleServer).credential(credential).group(group).hardwareId(hardwareId).identity(identity).osFamily(osFamily).osVersion(osVersion).imageId(imageId).instanceType(instanceType).locationId(locationId).number(number).nodeOptions(CloudUtils.parseProviderOptions(options)).owner(owner).adminAccess(!disableAdminAccess).publicKeyFile(publicKeyFile).contextName(contextName).providerName(providerName).apiName(apiName).user(user).password(password).proxyUri(proxyUri != null ? proxyUri : fabricService.getMavenRepoURI()).zookeeperUrl(fabricService.getZookeeperUrl()).zookeeperPassword(isEnsembleServer && zookeeperPassword != null ? zookeeperPassword : fabricService.getZookeeperPassword()).jvmOpts(jvmOpts).environmentalVariable(environmentalVariables).version(version).withUser(newUser, newUserPassword, newUserRole).profiles(getProfileNames()).dataStoreProperties(getDataStoreProperties()).uploadDistribution(!distributionUploadDisable);
    if (path != null && !path.isEmpty()) {
        builder.path(path);
    }
    CreateContainerMetadata[] metadatas = fabricService.createContainers(builder.build(), new PrintStreamCreationStateListener(System.out));
    if (isEnsembleServer && metadatas != null && metadatas.length > 0 && metadatas[0].isSuccess()) {
        ShellUtils.storeZookeeperPassword(session, metadatas[0].getCreateOptions().getZookeeperPassword());
    }
    // display containers
    displayContainers(metadatas);
    return null;
}
Also used : PrintStreamCreationStateListener(io.fabric8.internal.PrintStreamCreationStateListener) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) CreateJCloudsContainerOptions(io.fabric8.service.jclouds.CreateJCloudsContainerOptions)

Example 45 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class FabricDiscovery method addingService.

@Override
public CuratorFramework addingService(ServiceReference<CuratorFramework> reference) {
    CuratorFramework curator = context.getService(reference);
    try {
        logger.debug("CuratorFramework found, starting group");
        GroupFactory factory = new ZooKeeperGroupFactory(curator);
        singleton = factory.createGroup("/fabric/registry/clusters/elasticsearch/" + clusterName.value(), ESNode.class);
        singleton.add(this);
        singleton.update(new ESNode(clusterName.value(), localNode, false));
        singleton.start();
    } catch (Exception e) {
        LOG.error("Error starting group", e);
    }
    return curator;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) GroupFactory(io.fabric8.groups.GroupFactory) ZooKeeperGroupFactory(io.fabric8.groups.internal.ZooKeeperGroupFactory) ZooKeeperGroupFactory(io.fabric8.groups.internal.ZooKeeperGroupFactory) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchIllegalStateException(org.elasticsearch.ElasticsearchIllegalStateException) IOException(java.io.IOException)

Aggregations

Test (org.junit.jupiter.api.Test)32 Test (org.junit.Test)29 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)17 Map (java.util.Map)16 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 List (java.util.List)14 File (java.io.File)13 HashMap (java.util.HashMap)13 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)12 Collectors (java.util.stream.Collectors)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)10 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)9 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)9 CuratorFramework (org.apache.curator.framework.CuratorFramework)9 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)8 Pod (io.fabric8.kubernetes.api.model.Pod)8 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)8 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)8