Skip to content

Commit

Permalink
Remove default localhost vhost #189
Browse files Browse the repository at this point in the history
  • Loading branch information
vbradnitski committed Dec 6, 2023
1 parent 6ee75e3 commit 59cbb75
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 57 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version =
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version = "5.9.3" }

fabric8-kubernetes-clientapi = { module = "io.fabric8:kubernetes-client-api", version = "6.6.2" }
fabric8-kubernetes-client = { module = "io.fabric8:kubernetes-client", version = "6.6.0" }
fabric8-kubernetes-client = { module = "io.fabric8:kubernetes-client", version = "6.6.2" }
fabric8-kubernetes-servermock = { module = "io.fabric8:kubernetes-server-mock", version = "6.6.2" }
mockito-core = { module = "org.mockito:mockito-core", version = "5.4.0" }
mockito-inline = { module = "org.mockito:mockito-inline", version = "5.4.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
import com.enonic.kubernetes.kubernetes.Searchers;
import com.enonic.kubernetes.kubernetes.commands.K8sLogHelper;
import com.enonic.kubernetes.operator.Operator;

import com.google.common.base.Function;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath;
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
import io.fabric8.kubernetes.api.model.networking.v1.IngressRule;
import io.quarkus.runtime.StartupEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand All @@ -26,6 +30,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static com.enonic.kubernetes.common.Configuration.cfgBool;
import static com.enonic.kubernetes.common.Configuration.cfgLong;
import static com.enonic.kubernetes.common.Configuration.cfgStr;
import static com.enonic.kubernetes.kubernetes.Predicates.inNamespace;
Expand Down Expand Up @@ -60,9 +65,7 @@ void onStart( @Observes StartupEvent ev )
@Override
public void run()
{
searchers.xp7Config().stream().collect( groupingBy( r -> r.getMetadata().getNamespace() ) ).
keySet().
forEach( this::handle );
searchers.xp7Config().stream().collect( groupingBy( r -> r.getMetadata().getNamespace() ) ).keySet().forEach( this::handle );
}

public static Set<Mapping> getAnnotationMappings( final Ingress ingress )
Expand All @@ -73,19 +76,25 @@ public static Set<Mapping> getAnnotationMappings( final Ingress ingress )

// Collect all mapping prefixes
Set<String> mPrefix = new HashSet<>();
for (Map.Entry<String, String> e : annotations.entrySet()) {
if (e.getKey().startsWith( prefix ) && e.getKey().endsWith( ".source" )) {
for ( Map.Entry<String, String> e : annotations.entrySet() )
{
if ( e.getKey().startsWith( prefix ) && e.getKey().endsWith( ".source" ) )
{
mPrefix.add( e.getKey().replace( ".source", "" ) );
}
}

// Create mappings
for (String m : mPrefix) {
for ( String m : mPrefix )
{
Function<String, String> g = ( k ) -> annotations.get( m + "." + k );
try {
try
{
String name = m.replace( prefix, ingress.getMetadata().getName() + "-" );
result.add( MappingImpl.of( name, g.apply( "host" ), g.apply( "source" ), g.apply( "target" ), g.apply( "idproviders" ) ) );
} catch (Exception e) {
}
catch ( Exception e )
{
log.warn( String.format( "Invalid vhost mappings on ingress '%s'", ingress.getMetadata().getName() ) );
}
}
Expand All @@ -97,36 +106,39 @@ public void handle( final String namespace )
{
// Trigger update on each vHost config in the namespace
final String file = cfgStr( "operator.charts.values.files.vhosts" );
searchers.xp7Config().stream().
filter( inNamespace( namespace ) ).
filter( isDeleted().negate() ).
filter( xp7config -> Objects.equals( xp7config.getSpec().getFile(), file ) ).
forEach( xp7Config -> handle( namespace, xp7Config ) );
searchers.xp7Config()
.stream()
.filter( inNamespace( namespace ) )
.filter( isDeleted().negate() )
.filter( xp7config -> Objects.equals( xp7config.getSpec().getFile(), file ) )
.forEach( xp7Config -> handle( namespace, xp7Config ) );
}

private void handle( final String namespace, final Xp7Config xp7Config )
{
// Create a list ['all', '<nodeGroup>']
List<String> nodeGroups = Arrays.asList( cfgStr( "operator.charts.values.allNodesKey" ), xp7Config.getSpec().getNodeGroup() );
final List<String> nodeGroups = Arrays.asList( cfgStr( "operator.charts.values.allNodesKey" ), xp7Config.getSpec().getNodeGroup() );

// Collect all relevant ingresses
List<Ingress> ingresses = searchers.ingress().stream().
filter( inSameNamespaceAs( xp7Config ) ).
filter( isDeleted().negate() ).
filter( ingress -> hasMappingsWithNodeGroups( ingress, nodeGroups ) ).
collect( Collectors.toList() );
final List<Ingress> ingresses = searchers.ingress()
.stream()
.filter( inSameNamespaceAs( xp7Config ) )
.filter( isDeleted().negate() )
.filter( ingress -> hasMappingsWithNodeGroups( ingress, nodeGroups ) )
.collect( Collectors.toList() );

// Create new data
String data = createVHostData( namespace, ingresses, nodeGroups );
final String data = createVHostData( namespace, ingresses, nodeGroups );

// Update if needed
if (!Objects.equals( xp7Config.getSpec().getData(), data )) {
K8sLogHelper.logEdit( clients.xp7Configs().
inNamespace( xp7Config.getMetadata().getNamespace() ).
withName( xp7Config.getMetadata().getName() ), c -> {
c.getSpec().withData( data );
return c;
} );
if ( !Objects.equals( xp7Config.getSpec().getData(), data ) )
{
K8sLogHelper.logEdit(
clients.xp7Configs().inNamespace( xp7Config.getMetadata().getNamespace() ).withName( xp7Config.getMetadata().getName() ),
c -> {
c.getSpec().withData( data );
return c;
} );
}
}

Expand All @@ -135,24 +147,31 @@ private boolean hasMappingsWithNodeGroups( final Ingress ingress, final List<Str
Set<Mapping> targetMappings = getAnnotationMappings( ingress );

// No mappings
if (targetMappings.size() == 0) {
if ( targetMappings.size() == 0 )
{
return false;
}

// No rules
if (ingress.getSpec().getRules() == null) {
if ( ingress.getSpec().getRules() == null )
{
return false;
}

for (IngressRule rule : ingress.getSpec().getRules()) {
for ( IngressRule rule : ingress.getSpec().getRules() )
{
// No paths
if (rule.getHttp() == null || rule.getHttp().getPaths() == null) {
if ( rule.getHttp() == null || rule.getHttp().getPaths() == null )
{
return false;
}

for (HTTPIngressPath path : rule.getHttp().getPaths()) {
for ( HTTPIngressPath path : rule.getHttp().getPaths() )
{
// No backend
if (path.getBackend() == null || path.getBackend().getService().getName() == null || path.getBackend().getService().getPort() == null) {
if ( path.getBackend() == null || path.getBackend().getService().getName() == null ||
path.getBackend().getService().getPort() == null )
{
return false;
}

Expand All @@ -161,7 +180,8 @@ private boolean hasMappingsWithNodeGroups( final Ingress ingress, final List<Str
Integer port = path.getBackend().getService().getPort().getNumber();

// Only if service is the same as a nodegroup and port is 8080
if (nodeGroups.contains( service ) && port == 8080) {
if ( nodeGroups.contains( service ) && port == 8080 )
{
return true;
}
}
Expand All @@ -175,46 +195,54 @@ private String createVHostData( final String namespace, final List<Ingress> ingr
{
// Create config string builder
StringBuilder sb;
Optional<ConfigMap> cm = searchers.configMap().stream().
filter( inNamespace( namespace ) ).
filter( withName( "extra-config" ) ).
findFirst();
Optional<ConfigMap> cm =
searchers.configMap().stream().filter( inNamespace( namespace ) ).filter( withName( "extra-config" ) ).findFirst();

// Try to use vhost defaults
if (cm.isPresent() && cm.get().getData().containsKey( "vhost-defaults.cfg" )) {
sb = new StringBuilder( cm.get().getData().get( "vhost-defaults.cfg" ) );
} else {
log.warn( String.format( "Could not find default vhost configuration in NS '%s'", namespace ) );
sb = new StringBuilder( "enabled = true" );
}
if ( cfgBool( "operator.charts.values.settings.enableDefaultVhost" ) )
{
if ( cm.isPresent() && cm.get().getData().containsKey( "vhost-defaults.cfg" ) )
{
sb = new StringBuilder( cm.get().getData().get( "vhost-defaults.cfg" ) );
// Iterate over all ingresses
for ( Ingress ingress : ingresses )
{
addVHostMappings( sb, ingress, nodeGroups );
}
return sb.toString();
}

// Iterate over all ingresses
for (Ingress ingress : ingresses) {
addVHostMappings( sb, ingress, nodeGroups );
log.warn( String.format( "Could not find default vhost configuration in NS '%s'", namespace ) );
}

return sb.toString();
return "";
}

private void addVHostMappings( final StringBuilder sb, final Ingress ingress, final List<String> nodeGroups )
{
Set<Mapping> mappings = getAnnotationMappings( ingress );

// Iterate over rules
for (IngressRule rule : ingress.getSpec().getRules()) {
for ( IngressRule rule : ingress.getSpec().getRules() )
{
// Iterate over mappings
for (Mapping mapping : mappings) {
for ( Mapping mapping : mappings )
{
// Set rule host if missing
if (mapping.host() == null) {
if ( mapping.host() == null )
{
mapping = MappingImpl.copyOf( mapping ).withHost( rule.getHost() );
}

// If host matches
if (mapping.host().equals( rule.getHost() )) {
if ( mapping.host().equals( rule.getHost() ) )
{
// Iterate over paths
for (HTTPIngressPath path : rule.getHttp().getPaths()) {
for ( HTTPIngressPath path : rule.getHttp().getPaths() )
{
// Path matches mapping
if (nodeGroups.contains( path.getBackend().getService().getName() ) && mapping.source().equals( path.getPath() )) {
if ( nodeGroups.contains( path.getBackend().getService().getName() ) && mapping.source().equals( path.getPath() ) )
{
addVHostMappings( sb, mapping );
}
}
Expand All @@ -229,9 +257,11 @@ private void addVHostMappings( final StringBuilder sb, final Mapping m )
sb.append( String.format( "mapping.%s.host=%s\n", m.name(), m.host() ) );
sb.append( String.format( "mapping.%s.source=%s\n", m.name(), m.source() ) );
sb.append( String.format( "mapping.%s.target=%s", m.name(), m.target() ) );
if (m.idProviders() != null) {
if ( m.idProviders() != null )
{
String postFix = "default";
for (String idProvider : m.idProviders().split( "," )) {
for ( String idProvider : m.idProviders().split( "," ) )
{
sb.append( "\n" );
sb.append( String.format( "mapping.%s.idProvider.%s=%s", m.name(), idProvider, postFix ) );
postFix = "enabled";
Expand Down
1 change: 1 addition & 0 deletions java-operator/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ operator.charts.values.labelKeys.ingressVhostLoaded=enonic.cloud/vhost-loaded

operator.charts.values.settings.linkerd=false
operator.charts.values.settings.hzApiDiscovery=true
operator.charts.values.settings.enableDefaultVhost=false

############ Operator - Deployment #########

Expand Down
Loading

0 comments on commit 59cbb75

Please sign in to comment.